#import "ViewController.h" @interface ViewController () @property (strong,nonatomic)CALayer *subLayer; //聲明核心動畫子層 @end
- (void)viewDidLoad { [super viewDidLoad]; //建立子層 self.subLayer = [CALayer layer]; //設置子層大小 self.subLayer.bounds = CGRectMake(0, 0, 100, 100); //設置子層的位置 self.subLayer.position = CGPointMake(100, 100); //設置子層的背景顏色 self.subLayer.backgroundColor = [[UIColor redColor]CGColor]; //添加子層到根層 [self.view.layer addSublayer:self.subLayer]; }
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //建立關鍵幀動畫 CAKeyframeAnimation *keyframe = [[CAKeyframeAnimation alloc]init]; //設置變化屬性值爲透明度 keyframe.keyPath = @"opacity";
//設置每一幀動畫的透明度時刻值 keyframe.values = @[@1.0,@0.5,@0.0,@0.5,@1.0];
//設置動畫持續事件 keyframe.duration = 1.0f; //添加動畫到子層 [self.subLayer addAnimation:keyframe forKey:@"keyAnimation"]; //forkey:的參數能夠本身設置,就是一個標示符,區別開其餘的動畫 }
#import "ViewController.h" #define MAX_CLICKED_NUM 4 //觸摸的最大次數 @interface ViewController () @property (strong,nonatomic)NSMutableArray *points; //用於存放觸摸屏幕的次數 @property (strong,nonatomic)CALayer *subLayer;//聲明核心動畫子層 @end
- (void)viewDidLoad { [super viewDidLoad]; //初始化 self.points = [NSMutableArray array]; //建立子層 self.subLayer = [CALayer layer]; self.subLayer.bounds = CGRectMake(0, 0, 100, 100); self.subLayer.position = CGPointMake(100, 100); self.subLayer.backgroundColor = [[UIColor redColor]CGColor]; //添加子層到根層 [self.view.layer addSublayer:self.subLayer]; }
//觸摸事件的處理 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //獲取當前觸摸點 CGPoint location = [[touches anyObject]locationInView:self.view]; //判斷當前的點的個數 if (self.points.count < MAX_CLICKED_NUM) { //添加當前點 [self.points addObject:[NSValue valueWithCGPoint:location]]; } else { //建立關鍵幀動畫 CAKeyframeAnimation *keyanimation = [[CAKeyframeAnimation alloc]init]; //設置該變量爲位置position keyanimation.keyPath = @"position"; //設置每一幀動畫的位置值 keyanimation.values = self.points; //設置動畫持續時間 keyanimation.duration = 3.0f; //設置動畫不在恢復原狀 keyanimation.removedOnCompletion = NO; keyanimation.fillMode = kCAFillModeForwards; //設置代理 keyanimation.delegate = self; //添加動畫到層 [self.subLayer addAnimation:keyanimation forKey:@"keyanimation"]; } }
#pragma mark -動畫代理方法 -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { //關閉隱式動畫 [CATransaction begin]; [CATransaction setDisableActions:YES]; //停留在最後的位置 self.subLayer.position = [[self.points lastObject]CGPointValue]; //提交動畫事物 [CATransaction commit]; //刪除上一次動畫結束後保存的全部的數據 [self.points removeAllObjects]; }
#import "ViewController.h" @interface ViewController () @property (strong,nonatomic)CALayer *subLayer; //聲明核心動畫子層用做動畫層 @property (assign,nonatomic)CGMutablePathRef path; //聲明可變的繪圖路徑 @property (strong,nonatomic)CALayer *DrawLayer; //聲明核心動畫子層用來繪圖層 @end
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //建立繪圖子層 self.DrawLayer = [CALayer layer]; self.DrawLayer.bounds = self.view.bounds; self.DrawLayer.position = CGPointMake(0, 0); self.DrawLayer.anchorPoint = CGPointMake(0, 0); self.DrawLayer.backgroundColor = [[UIColor whiteColor]CGColor];
//設置繪圖子層的代理,它會調用協議中的繪圖方法 self.DrawLayer.delegate = self; //添加繪圖子層 [self.view.layer addSublayer:self.DrawLayer]; //建立動畫子層 self.subLayer = [CALayer layer]; self.subLayer.bounds = CGRectMake(0, 0, 100, 100); self.subLayer.position = CGPointMake(100, 100); self.subLayer.backgroundColor = [[UIColor redColor]CGColor]; //添加動畫子層 [self.view.layer addSublayer:self.subLayer]; }
//觸摸點擊事件的處理 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //獲取當前觸摸點 CGPoint location = [[touches anyObject]locationInView:self.view]; //建立路經 self.path = CGPathCreateMutable(); //將當前點加到路徑中去 CGPathMoveToPoint(self.path, NULL, location.x, location.y); }
//觸摸移動事件的處理 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //獲取當前觸摸點 CGPoint location = [[touches anyObject]locationInView:self.view]; //將當前點加到路徑中去 CGPathAddLineToPoint(self.path, NULL, location.x, location.y); //讓繪圖子層繪圖 [self.DrawLayer setNeedsDisplay]; }
//觸摸結束事件的處理 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //建立幀動畫 CAKeyframeAnimation *keyAnimation = [[CAKeyframeAnimation alloc]init]; //設置改變屬性值爲位置position keyAnimation.keyPath = @"position"; //設置動畫路徑爲繪製的路徑 keyAnimation.path = self.path; //設置動畫持續時間 keyAnimation.duration = 3.0f; //設置動畫結束時保持不變,不在恢復原狀 keyAnimation.removedOnCompletion = NO; keyAnimation.fillMode = kCAFillModeForwards; //設置幀動畫代理 keyAnimation.delegate = self; //添加幀動畫到動畫子層 [self.subLayer addAnimation:keyAnimation forKey:@"keyAnimation"]; }
#pragma mark -動畫代理方法 -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { //釋放路徑 CGPathRelease(self.path); }
#pragma mark -UILayer畫圖方法 -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { //添加路徑 CGContextAddPath(ctx, self.path); //設置顏色 CGContextSetStrokeColorWithColor(ctx, [[UIColor blueColor]CGColor]); //繪路徑 CGContextDrawPath(ctx, kCGPathStroke); }
運行結果以下:只給出起始位置和結束位置截圖,動畫過程很差捕捉,就不給出截圖了數組
開始時位置: 繪製路徑後,動畫沿路徑運行結束位置:動畫