帧动画CAKeyframeAnimation

Posted on 2016-07-07 16:19  柠檬片  阅读(109)  评论(0)    收藏  举报
 1 -(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
 2     
 3     
 4     //创建一个帧动画
 5     CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
 6     
 7     //设置属性
 8     anim.keyPath = @"position";
 9     
10 //    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 300, 400)];
11     
12     UIBezierPath *path = [UIBezierPath bezierPath];
13     
14     [path moveToPoint:CGPointMake(10, 50)];
15     
16     [path addLineToPoint:CGPointMake(300, 400)];
17     
18     
19     anim.path = path.CGPath;
20 
21     
22     //设置执行的次数
23     anim.repeatCount = MAXFLOAT;
24     
25     anim.autoreverses = YES;
26     
27     anim.duration = 1;
28     
29     //添加动画
30     [self.iconV.layer addAnimation:anim forKey:nil];
31     
32     
33 }
示例1(沿着某一路径走)
 1 //图标抖动
 2 - (void)values{
 3 
 4     //创建一个帧动画
 5     CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
 6     
 7     //设置属性
 8     anim.keyPath = @"transform.rotation";
 9     
10     //设置属性值.
11     anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5)),@(angle2Rad(-5))];
12     //设置执行的次数
13     anim.repeatCount = MAXFLOAT;
14     
15     anim.duration = 0.25;
16     
17     //添加动画
18     [self.iconV.layer addAnimation:anim forKey:nil];
19 
20 }
示例2(模仿苹果应用删除抖动效果)