此篇文章主要講述了CALayer與動(dòng)畫相關(guān)的一些操作屬性赋荆。
-
CALayer
的說(shuō)明
從中我們可以看出
CALayer
遵循了三個(gè)代理協(xié)議混狠,其中主要說(shuō)說(shuō)NSSecureCoding
和CAMediaTiming
NSSecureCoding
繼承自NSCoding
,NSSecureCoding
和NSCoding
是一樣的室抽,除了在解碼時(shí)要同時(shí)指定key
和要解碼的對(duì)象的類,如果要求的類和從文件中解碼出的對(duì)象的類不匹配,NSCode
r會(huì)拋出異常,告訴你數(shù)據(jù)已經(jīng)被篡改了猎醇。CAMediaTiming
中包含了很多屬性
/* The begin time of the object, in relation to its parent object, if
* applicable. Defaults to 0. */
public var beginTime: CFTimeInterval { get set }
/* The basic duration of the object. Defaults to 0. */
public var duration: CFTimeInterval { get set }
/* The rate of the layer. Used to scale parent time to local time, e.g.
* if rate is 2, local time progresses twice as fast as parent time.
* Defaults to 1. */
public var speed: Float { get set }
/* Additional offset in active local time. i.e. to convert from parent
* time tp to active local time t: t = (tp - begin) * speed + offset.
* One use of this is to "pause" a layer by setting `speed' to zero and
* `offset' to a suitable value. Defaults to 0. */
public var timeOffset: CFTimeInterval { get set }
/* The repeat count of the object. May be fractional. Defaults to 0. */
public var repeatCount: Float { get set }
/* The repeat duration of the object. Defaults to 0. */
public var repeatDuration: CFTimeInterval { get set }
/* When true, the object plays backwards after playing forwards. Defaults
* to NO. */
public var autoreverses: Bool { get set }
/* Defines how the timed object behaves outside its active duration.
* Local time may be clamped to either end of the active duration, or
* the element may be removed from the presentation. The legal values
* are `backwards', `forwards', `both' and `removed'. Defaults to
* `removed'. */
public var fillMode: String { get set }
beginTime
繼承CAMediaTiming協(xié)議的對(duì)象的起始時(shí)間
duration
基本動(dòng)畫的持續(xù)時(shí)間
speed
動(dòng)畫的運(yùn)行速度,當(dāng)為0時(shí)會(huì)停止動(dòng)畫努溃,speed越大說(shuō)明動(dòng)畫執(zhí)行速度越快
timeOffset
動(dòng)畫的時(shí)間偏移硫嘶,也就是上次動(dòng)畫的暫停/繼續(xù) 距離本次動(dòng)畫的繼續(xù)/暫停的時(shí)間差
repeatCount
重復(fù)次數(shù)
repeatDuration
重復(fù)的時(shí)間
autoreverses
是否會(huì)回到原來(lái)的位置
fillMode
CAFillModeRemoved
這個(gè)是默認(rèn)值,也就是說(shuō)當(dāng)動(dòng)畫開始前和動(dòng)畫結(jié)束后,動(dòng)畫對(duì)layer都沒有影響,動(dòng)畫結(jié)束后,layer會(huì)恢復(fù)到之前的狀態(tài)
kCAFillModeForwards
當(dāng)動(dòng)畫結(jié)束后,layer會(huì)一直保持著動(dòng)畫最后的狀態(tài)
kCAFillModeBackwards
這個(gè)和kCAFillModeForwards
是相對(duì)的,就是在動(dòng)畫開始前,你只要將動(dòng)畫加入了一個(gè)layer,layer便立即進(jìn)入動(dòng)畫的初始狀態(tài)并等待動(dòng)畫開始.你可以這樣設(shè)定試代碼,將一個(gè)動(dòng)畫加入一個(gè)layer的時(shí)候延遲5秒執(zhí)行.然后就會(huì)發(fā)現(xiàn)在動(dòng)畫沒有開始的時(shí)候,只要?jiǎng)赢嫳患尤肓薼ayer,layer便處于動(dòng)畫初始狀態(tài)
kCAFillModeBoth
理解了上面兩個(gè),這個(gè)就很好理解了,這個(gè)其實(shí)就是上面兩個(gè)的合成.動(dòng)畫加入后開始之前,layer便處于動(dòng)畫初始狀態(tài),動(dòng)畫結(jié)束后layer保持動(dòng)畫最后的狀態(tài).
- 下面進(jìn)行實(shí)例演習(xí)
做一個(gè)不停旋轉(zhuǎn)的動(dòng)畫,通過(guò)點(diǎn)擊事件控制動(dòng)畫的暫臀嗨埃或播放
///頭像旋轉(zhuǎn),開始動(dòng)畫
fileprivate func beginAnimation() {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotateAnimation.fromValue = 0
rotateAnimation.toValue = (Double.pi * 2)
rotateAnimation.duration = 20
rotateAnimation.repeatCount = MAXFLOAT
singerImageView.layer.add(rotateAnimation, forKey: "")
}
寫一個(gè)CALayer的分類沦疾,控制動(dòng)畫的暫停與繼續(xù)
extension CALayer {
///暫停動(dòng)畫
func pauseAnimation() {
//取出當(dāng)前時(shí)間,轉(zhuǎn)成動(dòng)畫暫停的時(shí)間
let pausedTime = self.convertTime(CACurrentMediaTime(), from: nil)
//設(shè)置動(dòng)畫運(yùn)行速度為0
self.speed = 0.0;
//設(shè)置動(dòng)畫的時(shí)間偏移量,指定時(shí)間偏移量的目的是讓動(dòng)畫定格在該時(shí)間點(diǎn)的位置
self.timeOffset = pausedTime
}
///恢復(fù)動(dòng)畫
func resumeAnimation() {
//獲取暫停的時(shí)間差
let pausedTime = self.timeOffset
self.speed = 1.0
self.timeOffset = 0.0
self.beginTime = 0.0
//用現(xiàn)在的時(shí)間減去時(shí)間差,就是之前暫停的時(shí)間,從之前暫停的時(shí)間開始動(dòng)畫
let timeSincePause = self.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
self.beginTime = timeSincePause
}
}
效果圖如下: