標籤:
一:組動畫簡單說明
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 *)event11 {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)viewDidLoad10 {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 添加一個image19 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 添加一個button25 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)flag41 {42 // 取齣動畫類型43 NSString *type = [anim valueForKey:@"animationType"];44 45 if ([type isEqualToString:@"translationTo"]) {46 // 取出目標點 並 設定self.center47 self.myView.center = [[anim valueForKey:@"targetPoint"]CGPointValue];48 }49 }50 51 #pragma mark - 手勢識別監聽方法52 - (void)longTapAction:(UILongPressGestureRecognizer *)recognizer53 {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 *)recognizer61 {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 }
iOS開發——動畫編程OC篇&(五)動畫組