1 #import "NJViewController.h" 2 3 @interface NJViewController () 4 5 @property (weak, nonatomic) IBOutlet UIView *customView; 6 - (IBAction)btnClick:(id)sender; 7 @end 8 9 @implementation NJViewController 10 11 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 12 { 13 14 [self test1]; 15 // [self test1]; 16 } 17 18 - (void)test1 19 { 20 // 1.创建核心动画 21 CAKeyframeAnimation *keyAnima = [CAKeyframeAnimation animation]; 22 // 1.1告诉系统执行什么动画 23 keyAnima.keyPath = @"position"; 24 25 CGMutablePathRef path = CGPathCreateMutable(); 26 CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 100, 200, 200)); 27 28 keyAnima.path = path; 29 CGPathRelease(path); 30 31 // 1.2保存执行完之后的状态 32 // 1.2.1执行完之后不删除动画 33 keyAnima.removedOnCompletion = NO; 34 // 1.2.2执行完之后保存最新的状态 35 keyAnima.fillMode = kCAFillModeForwards; 36 37 // 1.3设置动画时间 38 keyAnima.duration = 2; 39 // 2.观察动画什么时候开始执行, 以及什么时候执行完毕 40 keyAnima.delegate = self; 41 // 3.添加核心动画 42 [self.customView.layer addAnimation:keyAnima forKey:@"abc"]; 43 } 44 45 - (IBAction)btnClick:(id)sender { 46 47 // 停止动画 48 [self.customView.layer removeAnimationForKey:@"abc"]; 49 } 50 51 52 - (void)test 53 { 54 // 1.创建核心动画 55 CAKeyframeAnimation *keyAnima = [CAKeyframeAnimation animation]; 56 // 1.1告诉系统执行什么动画 57 keyAnima.keyPath = @"position"; 58 // NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(0, 100)]; 59 NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 60 NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)]; 61 NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)]; 62 NSValue *v5 = [NSValue valueWithCGPoint:CGPointMake(0, 100)]; 63 64 keyAnima.values = @[v2, v3, v4, v5]; 65 66 // keyAnima.keyTimes = @[@(0.5) ,@(0.5), @(0.5)]; 67 68 keyAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 69 70 // 1.2保存执行完之后的状态 71 // 1.2.1执行完之后不删除动画 72 keyAnima.removedOnCompletion = NO; 73 // 1.2.2执行完之后保存最新的状态 74 keyAnima.fillMode = kCAFillModeForwards; 75 76 // 1.3设置动画时间 77 keyAnima.duration = 2; 78 79 // 2.观察动画什么时候开始执行, 以及什么时候执行完毕 80 keyAnima.delegate = self; 81 82 83 // 2.添加核心动画 84 [self.customView.layer addAnimation:keyAnima forKey:nil]; 85 } 86 87 - (void)animationDidStart:(CAAnimation *)anim 88 { 89 NSLog(@"animationDidStart"); 90 } 91 92 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 93 { 94 NSLog(@"animationDidStop"); 95 } 96 97 98 @end
时间: 2024-10-15 05:53:06