CABasicAnimation
當(dāng)有多個(gè)動(dòng)畫時(shí),區(qū)分動(dòng)畫的方式:
- 如果添加動(dòng)畫的視圖圖層是全局的尼酿,
//添加動(dòng)畫是設(shè)置key
[self.view.layer addAnimation:animation forKey:@"animationKey"];
//在代理中植影,通過(guò)animationForKey:進(jìn)行比較
[anim isEqual:[self.view.layer animationForKey:@"animationKey"]]
*CAAnimation實(shí)現(xiàn)了KVC協(xié)議思币,可以通過(guò)key-value為動(dòng)畫添加標(biāo)識(shí)
//添加key-value
[animation setValue:@"animation" forKey:@"animationKey"];
//在代理中,進(jìn)行比較
[[anim valueForKey:@"animationKey"] isEqualToString:@"animation"]
CAKeyframeAnimation - 關(guān)鍵幀動(dòng)畫
*可以設(shè)置一連串個(gè)隨意的值做動(dòng)畫(values)
-
關(guān)鍵幀動(dòng)畫
在動(dòng)畫開(kāi)始的時(shí)候回突然調(diào)轉(zhuǎn)到第一針
惶我,在結(jié)束的時(shí)候突然恢復(fù)到原始的值
博投,所以為了動(dòng)畫的平滑,我們通常在動(dòng)畫的開(kāi)始和結(jié)束的關(guān)鍵幀
設(shè)置為當(dāng)前屬性的值
听怕。 - 如果設(shè)置結(jié)束和開(kāi)始值不同的動(dòng)畫,需要在動(dòng)畫啟動(dòng)之前手動(dòng)更新屬性和最后一幀保持一致虑绵。
- 使用CGPath設(shè)置動(dòng)畫
-
animation.rotationMode = kCAAnimationRotateAuto
根據(jù)切線方向自動(dòng)旋轉(zhuǎn)
CAAnimationGroup - 動(dòng)畫組
一個(gè)繼承于CAAnimation的子類尿瞭,添加了一個(gè)
animations
數(shù)組的屬性,用來(lái)組合其他的動(dòng)畫翅睛。
CATransition - 過(guò)渡
- 對(duì)于單獨(dú)存在的圖層声搁,我們可以通過(guò)實(shí)現(xiàn)圖層的
actionForLayer:forKey:
委托方法黑竞,或者提供一個(gè)actions
字典來(lái)控制隱式動(dòng)畫 - 對(duì)于UIView的跟圖層,因?yàn)榻昧穗[式動(dòng)畫酥艳,所以我們可以使用
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];
- 過(guò)渡動(dòng)畫和之前的屬性動(dòng)畫或者動(dòng)畫組添加到圖層上的方式一致摊溶,都是通過(guò)
-addAnimation:forKey:
方法。但是和屬性動(dòng)畫不同的是充石,對(duì)指定的圖層一次只能使用一次CATransition
。無(wú)論你對(duì)動(dòng)畫的鍵設(shè)置成什么值骤铃,過(guò)渡動(dòng)畫都會(huì)對(duì)它的鍵設(shè)置成"transition" -
CATransition
并不作用于指定的圖層屬性拉岁,這就是說(shuō)你可以在即使不能準(zhǔn)確得知改變了什么的情況下對(duì)圖層做動(dòng)畫,例如惰爬,在不知道UITableView哪一行被添加或者刪除的情況下喊暖,直接就可以平滑地刷新它
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
[self.tabBarController.view.layer addAnimation:transition forKey:nil];
}
- CATransition是一種對(duì)那些不太好做平滑動(dòng)畫屬性的強(qiáng)大工具
自定義動(dòng)畫
+ (void)transitionWithView: duration: options:animations: completion:
+ (void)transitionFromView: toView: duration: options: completion:
動(dòng)畫過(guò)程中取消動(dòng)畫
通過(guò)
-addAnimation: forKey:
中的key
來(lái)移除動(dòng)畫,-removeAnimationForKey:
簡(jiǎn)單記錄
實(shí)現(xiàn)動(dòng)畫的方式
- CATransaction: begin,commit
- UIView : [UIView beginAnimations:nil context:nil];
- UIView: animateWithDuration,transitionWithView,transitionFromView等對(duì)圖層樹(shù)的動(dòng)畫
- CATransition: 兩種:1:actions字典 2:addAnimation:forKey:
- CABasicAnimation:keyPath 屬性動(dòng)畫
- CAKeyframeAnimation:values 一連串動(dòng)畫撕瞧,數(shù)組的初始和結(jié)束 陵叽,注意動(dòng)畫銜接平滑; path可以繪制一段動(dòng)畫路徑
- CAAnimationGroup: animations組合動(dòng)畫