Core Animation晾匠,中文翻譯為核心動(dòng)畫茶袒,它是一組非常強(qiáng)大的動(dòng)畫處理API,使用它能做出非常炫麗的動(dòng)畫效果凉馆,而且往往是事半功倍薪寓。也就是說,使用少量的代碼就可以實(shí)現(xiàn)非常強(qiáng)大的功能澜共。
Core Animation可以用在Mac OS X和iOS平臺(tái)向叉。
Core Animation的動(dòng)畫執(zhí)行過程都是在后臺(tái)操作的,不會(huì)阻塞主線程嗦董。
要注意的是母谎,Core Animation是直接作用在CALayer上的,并非UIView京革。
一: 基本概念
1.1 什么是核心動(dòng)畫
Core Animation(核心動(dòng)畫)是一組功能強(qiáng)大奇唤、效果華麗的動(dòng)畫API,無論在iOS系統(tǒng)或者在你開發(fā)的App中匹摇,都有大量應(yīng)用咬扇。
可以看到,核心動(dòng)畫位于UIKit的下一層廊勃,相比UIView動(dòng)畫懈贺,它可以實(shí)現(xiàn)更復(fù)雜的動(dòng)畫效果。
核心動(dòng)畫作用在CALayer(Core animation layer)
上供搀,CALayer從概念上類似UIView隅居,我們可以將UIView看成是一種特殊的CALayer
(可以響應(yīng)事件)
實(shí)際上,每一個(gè)view都有其對(duì)應(yīng)的layer葛虐,這個(gè)layer是root layer:
@property(nonatomic,readonly,strong) CALayer *layer;
給view加上動(dòng)畫胎源,本質(zhì)上是對(duì)其layer進(jìn)行操作,layer包含了各種支持動(dòng)畫的屬性屿脐,動(dòng)畫則包含了屬性變化的值涕蚤、變化的速度、變化的時(shí)間等等的诵,兩者結(jié)合產(chǎn)生動(dòng)畫的過程万栅。
#######1.2 核心動(dòng)畫和UIView動(dòng)畫
核心動(dòng)畫和UIView動(dòng)畫的對(duì)比:UIView動(dòng)畫可以看成是對(duì)核心動(dòng)畫的封裝,和UIView動(dòng)畫不同的是西疤,通過核心動(dòng)畫改變layer的狀態(tài)(比如position)烦粒,動(dòng)畫執(zhí)行完畢后實(shí)際上是沒有改變的(表面上看起來已改變)。
核心動(dòng)畫的優(yōu)點(diǎn)有:
- 1)性能強(qiáng)大,使用硬件加速扰她,可以同時(shí)向多個(gè)圖層添加不同的動(dòng)畫效果
- 2)接口易用兽掰,只需要少量的代碼就可以實(shí)現(xiàn)復(fù)雜的動(dòng)畫效果。
- 3)運(yùn)行在后臺(tái)線程中徒役,在動(dòng)畫過程中可以響應(yīng)交互事件(UIView動(dòng)畫默認(rèn)動(dòng)畫過程中不響應(yīng)交互事件)孽尽。
#######1.3 核心動(dòng)畫類的層次結(jié)構(gòu)
CAAnimation
是所有動(dòng)畫對(duì)象的父類,實(shí)現(xiàn)CAMediaTiming協(xié)議忧勿,負(fù)責(zé)控制動(dòng)畫的時(shí)間杉女、速度和時(shí)間曲線等等,是一個(gè)抽象類鸳吸,不能直接使用熏挎。
- CAPropertyAnimation :是CAAnimation的子類,它支持動(dòng)畫地顯示圖層的keyPath层释,一般不直接使用婆瓜。
- iOS9.0之后新增CASpringAnimation類快集,它實(shí)現(xiàn)彈簧效果的動(dòng)畫贡羔,是CABasicAnimation的子類。
綜上个初,核心動(dòng)畫類中可以直接使用的類有:
CABasicAnimation 基本動(dòng)畫
CAKeyframeAnimation
CATransition 轉(zhuǎn)場(chǎng)動(dòng)畫
CAAnimationGroup 組動(dòng)畫
CASpringAnimation 彈簧動(dòng)畫
1.4 核心動(dòng)畫類的核心方法
#######1.4.1 初始化CAAnimation對(duì)象
一般使用animation方法生成實(shí)例
+ (instancetype)animation;
如果是CAPropertyAnimation的子類乖寒,還可以通過animationWithKeyPath生成實(shí)例
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
#######1.4.2 設(shè)置動(dòng)畫的相關(guān)屬性
設(shè)置動(dòng)畫的執(zhí)行時(shí)間,執(zhí)行曲線院溺,keyPath的目標(biāo)值楣嘁,代理等等
#######1.4.3 動(dòng)畫的添加和移除
調(diào)用CALayer的addAnimation:forKey:方法將動(dòng)畫添加到CALayer中,這樣動(dòng)畫就開始執(zhí)行了
- (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key;
調(diào)用CALayer的removeAnimation方法停止CALayer中的動(dòng)畫
- (void)removeAnimationForKey:(NSString *)key;
- (void)removeAllAnimations;
二: 核心動(dòng)畫類的常用屬性
duration:動(dòng)畫的持續(xù)時(shí)間
repeatCount:動(dòng)畫的重復(fù)次數(shù)
timingFunction:動(dòng)畫的時(shí)間節(jié)奏控制
2.1 我們動(dòng)畫的keyPath
keyPath:可以指定keyPath為CALayer的屬性值珍逸,并對(duì)它的值進(jìn)行修改逐虚,以達(dá)到對(duì)應(yīng)的動(dòng)畫效果,需要注意的是部分屬性值是不支持動(dòng)畫效果的谆膳。
- 以下是具有動(dòng)畫效果的keyPath:
//CATransform3D Key Paths : (example)transform.rotation.z
//rotation.x
//rotation.y
//rotation.z
//rotation 旋轉(zhuǎn)
//scale.x
//scale.y
//scale.z
//scale 縮放
//translation.x
//translation.y
//translation.z
//translation 平移
---------------------------------
//CGPoint Key Paths : (example)position.x
//x
//y
---------------------------------
//CGRect Key Paths : (example)bounds.size.width
//origin.x
//origin.y
//origin
//size.width
//size.height
//size
---------------------------------
//opacity
//backgroundColor
//cornerRadius
//borderWidth
//contents
---------------------------------
//Shadow Key Path:
//shadowColor
//shadowOffset
//shadowOpacity
//shadowRadius
2.2 timingFunction:動(dòng)畫的時(shí)間節(jié)奏控制
timingFunctionName的enum值如下:
kCAMediaTimingFunctionLinear 勻速
kCAMediaTimingFunctionEaseIn 慢進(jìn)
kCAMediaTimingFunctionEaseOut 慢出
kCAMediaTimingFunctionEaseInEaseOut 慢進(jìn)慢出
kCAMediaTimingFunctionDefault 默認(rèn)值(慢進(jìn)慢出)
2.3 fillMode:視圖在非Active時(shí)的行為
2.4 removedOnCompletion 動(dòng)畫執(zhí)行完畢后是否從圖層上移除
removedOnCompletion
:動(dòng)畫執(zhí)行完畢后是否從圖層上移除叭爱,默認(rèn)為YES(視圖會(huì)恢復(fù)到動(dòng)畫前的狀態(tài)),可設(shè)置為NO(圖層保持動(dòng)畫執(zhí)行后的狀態(tài)漱病,前提是fillMode設(shè)置為kCAFillModeForwards)
2.5 beginTime:動(dòng)畫延遲執(zhí)行時(shí)間(通過CACurrentMediaTime() + your time 設(shè)置)
2.6 delegate:代理
代理方法如下:
- (void)animationDidStart:(CAAnimation *)anim; //動(dòng)畫開始
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag; //動(dòng)畫結(jié)束
三: CABasicAnimation
CABasicAnimation
可以設(shè)定keyPath的起點(diǎn)买雾,終點(diǎn)的值,動(dòng)畫會(huì)沿著設(shè)定點(diǎn)進(jìn)行移動(dòng)杨帽,CABasicAnimation可以看成是只有兩個(gè)關(guān)鍵點(diǎn)的特殊的CAKeyFrameAnimation漓穿。
#######3.1 Scale的實(shí)例
func testScale() {
let ani = CABasicAnimation(keyPath: "transform.scale")
ani.fromValue = 0
ani.toValue = 2
ani.isRemovedOnCompletion = false
ani.fillMode = kCAFillModeForwards
ani.duration = 3
ani.repeatCount = MAXFLOAT
ani.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
showImgView.layer.add(ani, forKey: "transform.scale")
}
#######3.2 Rotation實(shí)例
func testRotation() {
let ani = CABasicAnimation(keyPath: "transform.rotation")
ani.fromValue = 0
ani.toValue = -M_PI
ani.isRemovedOnCompletion = false
ani.fillMode = kCAFillModeForwards
ani.duration = 3
ani.repeatCount = MAXFLOAT
ani.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
showImgView.layer.add(ani, forKey: "transform.rotation")
}
#######3.3 Position
func testPosition() {
let ani = CABasicAnimation(keyPath: "position")
ani.fromValue = CGPoint()
ani.toValue = view.center
ani.isRemovedOnCompletion = false
ani.fillMode = kCAFillModeForwards
ani.duration = 3
ani.repeatCount = MAXFLOAT
ani.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
showImgView.layer.add(ani, forKey: "position")
}
關(guān)鍵幀動(dòng)畫
一: CAKeyframeAnimation
可以設(shè)定keyPath起點(diǎn)、中間關(guān)鍵點(diǎn)(不止一個(gè))注盈、終點(diǎn)的值晃危,每一幀所對(duì)應(yīng)的時(shí)間,動(dòng)畫會(huì)沿著設(shè)定點(diǎn)進(jìn)行移動(dòng)老客。
CAKeyframeAnimation的重要屬性:
1僚饭, values:關(guān)鍵幀數(shù)組對(duì)象纠俭,里面每一個(gè)元素即為一個(gè)關(guān)鍵幀,動(dòng)畫會(huì)在對(duì)應(yīng)的時(shí)間段內(nèi)浪慌,依次執(zhí)行數(shù)組中每一個(gè)關(guān)鍵幀的動(dòng)畫冤荆。
2,path:動(dòng)畫路徑對(duì)象权纤,可以指定一個(gè)路徑钓简,在執(zhí)行動(dòng)畫時(shí)路徑會(huì)沿著路徑移動(dòng),Path在動(dòng)畫中只會(huì)影響視圖的Position汹想。
3外邓,keyTimes:設(shè)置關(guān)鍵幀對(duì)應(yīng)的時(shí)間點(diǎn),范圍:0-1古掏。如果沒有設(shè)置該屬性损话,則每一幀的時(shí)間平分。
二: 關(guān)鍵幀動(dòng)畫的實(shí)例
/// 圍繞四方形運(yùn)動(dòng)
func testFramePosition() {
let ani:CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
ani.duration = 4.0
ani.isRemovedOnCompletion = false
ani.fillMode = kCAFillModeForwards
ani.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut)
ani.repeatCount = MAXFLOAT
ani.duration = 3
let value1 = CGPoint(x: 100, y: 100)
let value2 = CGPoint(x: 400, y: 100)
let value3 = CGPoint(x: 400, y: 400)
let value4 = CGPoint(x: 100, y: 400)
let value5 = CGPoint(x: 100, y: 100)
ani.values = [value1, value2, value3, value4, value5]
showImgView.layer.add(ani, forKey: "position")
}
/// 圍繞path運(yùn)動(dòng)
func testCirclePosition() {
let ani:CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "position")
ani.isRemovedOnCompletion = false
ani.fillMode = kCAFillModeBoth
ani.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut)
ani.repeatCount = MAXFLOAT
ani.duration = 3
let path = CGMutablePath()
path.addEllipse(in: CGRect(x: 100, y: 100, width: 200, height: 200), transform: CGAffineTransform(scaleX: 1, y: 1))
ani.path = path
showImgView.layer.add(ani, forKey: "position")
}