![]()
1 //
2 // ViewController.m
3 // coreAnimation
4 //
5 // Created by ys on 15/11/21.
6 // Copyright (c) 2015年 ys. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 @interface ViewController ()
12 @property (weak, nonatomic) IBOutlet UIView *blackView;
13 @end
14
15 @implementation ViewController
16
17 - (void)viewDidLoad
18 {
19 [super viewDidLoad];
20
21 }
22 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
23 {
24 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
25
26 anim.keyPath = @"position";
27 anim.removedOnCompletion = NO;
28 anim.fillMode = kCAFillModeForwards;
29 anim.duration = 2.0;
30
31 CGMutablePathRef path = CGPathCreateMutable();
32 CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
33 anim.path = path;
34 CGPathRelease(path);
35
36 // 设置动画的执行节奏
37 // kCAMediaTimingFunctionEaseInEaseOut : 一开始比较慢, 中间会加速, 临近结束的时候, 会变慢
38 anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
39 anim.delegate = self;
40
41 [self.blackView.layer addAnimation:anim forKey:nil];
42 }
43
44 #pragma mark - 动画的代理方法
45 #pragma mark 动画开始的时候调用
46 - (void)animationDidStart:(CAAnimation *)anim
47 {
48 NSLog(@"animationDidStart");
49 }
50 #pragma mark 动画结束的时候调用
51 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
52 {
53 NSLog(@"animationDidStop");
54 }
55
56 - (void)testMove
57 {
58 // CABasicAnimation fromValue --> toValue
59
60 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
61
62 anim.keyPath = @"position";
63
64 NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];
65 NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
66 NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
67 NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];
68 anim.values = @[v1, v2, v3, v4];
69
70 // anim.keyTimes = @[@(0.5), @(0.25), @(0.25)];
71
72 anim.duration = 2.0;
73
74
75 anim.removedOnCompletion = NO;
76 anim.fillMode = kCAFillModeForwards;
77
78 [self.blackView.layer addAnimation:anim forKey:nil];
79 }
80
81
82 @end