CABasicAnimation
當有多個動畫時,區分動畫的方式:
- 如果添加動畫的視圖圖層是全局的,
//添加動畫是設置key
[self.view.layer addAnimation:animation forKey:@"animationKey"];
//在代理中,通過animationForKey:進行比較
[anim isEqual:[self.view.layer animationForKey:@"animationKey"]]
*CAAnimation實現了KVC協議,可以通過key-value為動畫添加標識
//添加key-value
[animation setValue:@"animation" forKey:@"animationKey"];
//在代理中,進行比較
[[anim valueForKey:@"animationKey"] isEqualToString:@"animation"]
CAKeyframeAnimation - 關鍵幀動畫
*可以設置一連串個隨意的值做動畫(values)
-
關鍵幀動畫
在動畫開始的時候回突然調轉到第一針
,在結束的時候突然恢復到原始的值
,所以為了動畫的平滑,我們通常在動畫的開始和結束的關鍵幀
設置為當前屬性的值
。 - 如果設置結束和開始值不同的動畫,需要在動畫啟動之前手動更新屬性和最后一幀保持一致。
- 使用CGPath設置動畫
-
animation.rotationMode = kCAAnimationRotateAuto
根據切線方向自動旋轉
CAAnimationGroup - 動畫組
一個繼承于CAAnimation的子類,添加了一個
animations
數組的屬性,用來組合其他的動畫。
CATransition - 過渡
- 對于單獨存在的圖層,我們可以通過實現圖層的
actionForLayer:forKey:
委托方法,或者提供一個actions
字典來控制隱式動畫 - 對于UIView的跟圖層,因為禁用了隱式動畫,所以我們可以使用
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
[self.imageView.layer addAnimation:transition forKey:nil];
UIImage *currentImage = self.imageView.image;
NSUInteger index = [self.images indexOfObject:currentImage];
index = (index + 1) % [self.images count];
self.imageView.image = self.images[index];
- 過渡動畫和之前的屬性動畫或者動畫組添加到圖層上的方式一致,都是通過
-addAnimation:forKey:
方法。但是和屬性動畫不同的是,對指定的圖層一次只能使用一次CATransition
。無論你對動畫的鍵設置成什么值,過渡動畫都會對它的鍵設置成"transition" -
CATransition
并不作用于指定的圖層屬性,這就是說你可以在即使不能準確得知改變了什么的情況下對圖層做動畫,例如,在不知道UITableView哪一行被添加或者刪除的情況下,直接就可以平滑地刷新它
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
[self.tabBarController.view.layer addAnimation:transition forKey:nil];
}
- CATransition是一種對那些不太好做平滑動畫屬性的強大工具
自定義動畫
+ (void)transitionWithView: duration: options:animations: completion:
+ (void)transitionFromView: toView: duration: options: completion:
動畫過程中取消動畫
通過
-addAnimation: forKey:
中的key
來移除動畫,-removeAnimationForKey:
簡單記錄
實現動畫的方式
- CATransaction: begin,commit
- UIView : [UIView beginAnimations:nil context:nil];
- UIView: animateWithDuration,transitionWithView,transitionFromView等對圖層樹的動畫
- CATransition: 兩種:1:actions字典 2:addAnimation:forKey:
- CABasicAnimation:keyPath 屬性動畫
- CAKeyframeAnimation:values 一連串動畫,數組的初始和結束 ,注意動畫銜接平滑; path可以繪制一段動畫路徑
- CAAnimationGroup: animations組合動畫