标签:
一:组动画简单说明
CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行
属性解析:
animations:用来保存一组动画对象的NSArray
默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间
二:分组动画代码示例
代码:
1 #import "YYViewController.h"
2
3 @interface YYViewController ()
4 @property (weak, nonatomic) IBOutlet UIView *iconView;
5
6 @end
7
8 @implementation NJViewController
9
10 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
11 {
12
13 // 平移动画
14 CABasicAnimation *a1 = [CABasicAnimation animation];
15 a1.keyPath = @"transform.translation.y";
16 a1.toValue = @(100);
17 // 缩放动画
18 CABasicAnimation *a2 = [CABasicAnimation animation];
19 a2.keyPath = @"transform.scale";
20 a2.toValue = @(0.0);
21 // 旋转动画
22 CABasicAnimation *a3 = [CABasicAnimation animation];
23 a3.keyPath = @"transform.rotation";
24 a3.toValue = @(M_PI_2);
25
26 // 组动画
27 CAAnimationGroup *groupAnima = [CAAnimationGroup animation];
28
29 groupAnima.animations = @[a1, a2, a3];
30
31 //设置组动画的时间
32 groupAnima.duration = 2;
33 groupAnima.fillMode = kCAFillModeForwards;
34 groupAnima.removedOnCompletion = NO;
35
36 [self.iconView.layer addAnimation:groupAnima forKey:nil];
37 }
38
39 @end
说明:平移-旋转-缩放作为一组动画一起执行。
执行效果:
1 /* 2 本次演练使用自定义视图的方式,动画的触发方式以手势识别来触发 3 4 1. 指定点平移动画 5 2. 路径平移动画 6 3. 贝塞尔曲线路径动画 7 4. 摇晃动画 8 */ 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 13 // 实例化动画视图 14 AnimationView *view = [[AnimationView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; 15 [self.view addSubview:view]; 16 self.myView = view; 17 18 // 给myView 添加一个image 19 UIImage *image = [UIImage imageNamed:@"头像1.png"]; 20 UIImageView *imageView = [[UIImageView alloc]initWithFrame:view.bounds]; 21 [imageView setImage:image]; 22 [view addSubview:imageView]; 23 24 // 给myView 添加一个button 25 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 26 [button setFrame:CGRectMake(10, 30, 80, 40)]; 27 [button setTitle:@"hello" forState:UIControlStateNormal]; 28 [view addSubview:button]; 29 30 // 手势识别监听 31 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; 32 [self.view addGestureRecognizer:tap]; 33 34 // 长按手势监听 35 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTapAction:)]; 36 [self.myView addGestureRecognizer:longPress]; 37 } 38 39 #pragma mark - 动画代理方法 40 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 41 { 42 // 取出动画类型 43 NSString *type = [anim valueForKey:@"animationType"]; 44 45 if ([type isEqualToString:@"translationTo"]) { 46 // 取出目标点 并 设置self.center 47 self.myView.center = [[anim valueForKey:@"targetPoint"]CGPointValue]; 48 } 49 } 50 51 #pragma mark - 手势识别监听方法 52 - (void)longTapAction:(UILongPressGestureRecognizer *)recognizer 53 { 54 // [(AnimationView *)recognizer.view shakeAnimation]; 55 CAKeyframeAnimation *anim = [AnimationView shakeAnimation]; 56 57 [self.myView.layer addAnimation:anim forKey:nil]; 58 } 59 60 - (void)tapAction:(UITapGestureRecognizer *)recognizer 61 { 62 // 取出手势的触摸点 63 CGPoint location = [recognizer locationInView:self.view]; 64 65 // 1. 实例化动画组 66 CAAnimationGroup *group = [[CAAnimationGroup alloc]init]; 67 68 CFTimeInterval duration = 2.0; 69 70 // 2. 定义动画组中的动画 71 // CAKeyframeAnimation *anim1 = [AnimationView moveRectWithDuration:duration from:self.myView.center to:location]; 72 CAKeyframeAnimation *anim1 = [AnimationView moveWithDuration:duration from:self.myView.center to:location controlPointCount:4]; 73 CABasicAnimation *anim2 = [AnimationView rotationWithDuration:duration from:0.0 to:2 * M_PI]; 74 75 CABasicAnimation *scale = [AnimationView scaleWithDuration:duration from:2.0 to:0.5]; 76 CABasicAnimation *opacity = [AnimationView opacityWithDuration:duration from:0.1 to:1.0]; 77 78 // 3. 将定义的动画添加到动画组 79 [group setAnimations:@[anim1, anim2, scale, opacity]]; 80 81 // 定义动画组执行的时间长度 82 [group setDuration:duration]; 83 84 // 5) 设置键值记录目标位置,以便动画结束后,修正位置 85 // 并不是所有的动画方法都需要设置目标点 86 [group setValue:@"translationTo" forKey:@"animationType"]; 87 [group setValue:[NSValue valueWithCGPoint:location] forKey:@"targetPoint"]; 88 [group setDelegate:self]; 89 90 // 4. 将动画组添加到视图的图层 91 [self.myView.layer addAnimation:group forKey:nil]; 92 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4552981.html