1 // 2 // ViewController.m 3 // 06 关键帧动画 4 // 5 // Created by ZhuJiaCong on 16/4/18. 6 // Copyright © 2016年 wxhl. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 { 13 CGMutablePathRef _path; 14 } 15 @property (weak, nonatomic) IBOutlet UIView *redView; 16 17 @end 18 19 @implementation ViewController 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 24 _redView.layer.cornerRadius = 10; 25 26 27 28 } 29 30 31 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 32 33 //关键帧动画 34 // CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 35 // 36 // //创建帧 37 // CGPoint point1 = CGPointMake(100, 100); 38 // CGPoint point2 = CGPointMake(150, 100); 39 // CGPoint point3 = CGPointMake(100, 150); 40 // CGPoint point4 = CGPointMake(150, 150); 41 // CGPoint point5 = CGPointMake(200, 100); 42 // CGPoint point6 = CGPointMake(100, 200); 43 // 44 // 45 // NSArray *array = @[[NSValue valueWithCGPoint:point1], 46 // [NSValue valueWithCGPoint:point2], 47 // [NSValue valueWithCGPoint:point3], 48 // [NSValue valueWithCGPoint:point4], 49 // [NSValue valueWithCGPoint:point5], 50 // [NSValue valueWithCGPoint:point6] 51 // ]; 52 // 53 // keyFrame.values = array; 54 // 55 // keyFrame.duration = 3; 56 // 57 // //将动画添加到图层上去 58 // [_redView.layer addAnimation:keyFrame forKey:@"keyFrame"]; 59 60 61 62 63 if (_path) { 64 CGPathRelease(_path); 65 _path = NULL; 66 } 67 68 //创建路径 69 _path = CGPathCreateMutable(); 70 //设置路径起点 71 UITouch *touch = [touches anyObject]; 72 CGPoint location = [touch locationInView:self.view]; 73 CGPathMoveToPoint(_path, NULL, location.x, location.y); 74 75 } 76 77 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 78 //获取触摸点 添加路径点 79 UITouch *touch = [touches anyObject]; 80 CGPoint location = [touch locationInView:self.view]; 81 82 CGPathAddLineToPoint(_path, NULL, location.x, location.y); 83 84 } 85 86 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 87 88 //执行动画 89 CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 90 keyFrame.path = _path; 91 92 93 keyFrame.duration = 5; 94 95 96 97 //速度控制函数 98 keyFrame.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 99 100 101 [_redView.layer addAnimation:keyFrame forKey:nil]; 102 103 } 104 105 106 107 - (void)didReceiveMemoryWarning { 108 [super didReceiveMemoryWarning]; 109 // Dispose of any resources that can be recreated. 110 } 111 112 @end
时间: 2024-10-10 06:07:24