標籤:參數 oop load validate bezier pst with drawing uikit
先說下當前我為處理動畫的思路: (新手上路, 老司機輕噴,如果有更好的實現方法請大神指教 感恩戴德)
#1. 分析動畫構成
#2. 如果是位移動畫則考慮使用BasicAnimation或者KeyframeAnimation實現, 需要的話再搭配easing 函式
#3. 比較複雜的動畫則考慮是否用UIBezierpath一幀幀來畫
今天我們模仿做一個情境切換載入等待動畫, 比如這樣的
我們分析下這張圖的構成
#1. 一個灰色的背景
#2. 一個白色的圓環
#3. 一個閉合的圓弧(白色部分)
看起來不是簡單的位移動畫了, 我們用UIBezierPath加CADisplayLink一幀一幀來畫試試看
灰色的背景,
這個比較簡單, 我們直接建立一個UIView子類, 背景顏色設定為灰色
白色的圓環,
可以用UIBezierPath直接畫一個圓,注意調整線的寬度 So easy
//添加外圓 UIBezierPath *apath = [UIBezierPath bezierPath]; apath.lineWidth = 5; [apath addArcWithCenter:CGPointMake(50, 50) radius:40 startAngle:0 endAngle:2 * M_PI clockwise:YES]; [apath stroke];
閉合的圓弧,
一樣用UIBezierPath, 先設定圓心 畫一個圓弧然後閉合路徑, _count是設定的一個變數, 有Controller中的計時器控制以達到動畫的效果
//先畫內圓 //設定線條 path.lineWidth = 5; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinRound; //設定圓心 [path moveToPoint:CGPointMake(_myWidth / 2, _myHeight / 2)]; //設定內切圓弧 [path addArcWithCenter:CGPointMake(_myWidth / 2, _myHeight / 2) radius:38 startAngle: M_PI * 3 / 2 endAngle:M_PI * 3 / 2 + 2 * M_PI / 300 * _count clockwise:YES]; //線路閉合 [path closePath]; [path fill];
要注意調整外圓和內閉合弧的線寬
然後在Controller中建立計時器, 改變_count的值達到動畫的效果
上代碼:
先建立一個UIView子類,
#import <UIKit/UIKit.h>@interface MyView : UIView@property (nonatomic, assign) CGFloat myWidth;@property (nonatomic, assign) CGFloat myHeight;@property (nonatomic, assign) NSInteger count;@end
在drawRect中添加圖案
#import "MyView.h"@implementation MyView- (id)initWithFrame:(CGRect)frame { CGRect myFrame = CGRectMake(frame.origin.x, 0, frame.size.width, frame.size.height); self = [super initWithFrame:myFrame]; if (self) { self.myWidth = frame.size.width; self.myHeight = frame.size.height; } return self;}- (void)drawRect:(CGRect)rect { // Drawing code //設定線條顏色 UIColor *color = [UIColor whiteColor]; [color set]; //畫內圓 UIBezierPath *path = [UIBezierPath bezierPath]; //設定線條 path.lineWidth = 5; path.lineCapStyle = kCGLineCapRound; path.lineJoinStyle = kCGLineJoinRound; //設定圓心 [path moveToPoint:CGPointMake(_myWidth / 2, _myHeight / 2)]; //設定內切圓弧 [path addArcWithCenter:CGPointMake(_myWidth / 2, _myHeight / 2) radius:38 startAngle: M_PI * 3 / 2 endAngle:M_PI * 3 / 2 + 2 * M_PI / 300 * _count clockwise:YES]; //線路閉合 [path closePath]; [path fill]; //添加外圓 UIBezierPath *apath = [UIBezierPath bezierPath]; apath.lineWidth = 5; [apath addArcWithCenter:CGPointMake(50, 50) radius:40 startAngle:0 endAngle:2 * M_PI clockwise:YES]; [apath stroke]; }@end
在Controller中調用
#import "ViewController.h"#import "MyView.h"@interface ViewController ()@property (nonatomic, strong) MyView *myView;@property (nonatomic, strong) CADisplayLink *displayLink;@property (nonatomic, assign) NSInteger count;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //建立計時器 _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(countOn)]; _displayLink.paused = YES; [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; //建立自訂View執行個體 _myView = [[MyView alloc] initWithFrame:CGRectMake(150, 0, 100, 100)]; _myView.backgroundColor = [UIColor grayColor]; _myView.count = 1; [self.view addSubview:_myView]; //在這裡可以添加額外動畫, 設定hud出現的方式 [UIView animateWithDuration:0.5 animations:^{ _myView.frame = CGRectMake(150, 150, 100, 100); _displayLink.paused = NO; }]; }//計時器事件, 修改動畫參數- (void)countOn { _count++; _myView.count = _count; if (_count > 290) { [_displayLink invalidate]; [_myView removeFromSuperview]; } //重繪 [_myView setNeedsDisplay];}
建立簡單動畫(一) --- 常規hud