iOS中CALayer動(dòng)畫的暫停與繼續(xù)

此篇文章主要講述了CALayer與動(dòng)畫相關(guān)的一些操作屬性。

  • CALayer的說(shuō)明

image.png

從中我們可以看出CALayer遵循了三個(gè)代理協(xié)議,其中主要說(shuō)說(shuō)NSSecureCodingCAMediaTiming
NSSecureCoding繼承自NSCoding,NSSecureCodingNSCoding是一樣的,除了在解碼時(shí)要同時(shí)指定key和要解碼的對(duì)象的類,如果要求的類和從文件中解碼出的對(duì)象的類不匹配,NSCoder會(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)畫的暫?;虿シ?/li>
    ///頭像旋轉(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
    }
}

效果圖如下:

animation.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,688評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,262評(píng)論 5 13
  • 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無(wú)非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,313評(píng)論 1 23
  • Core Animation Core Animation,中文翻譯為核心動(dòng)畫,它是一組非常強(qiáng)大的動(dòng)畫處理API,...
    45b645c5912e閱讀 3,153評(píng)論 0 21
  • CALayer - 在iOS中,你能看得見摸得著的東西基本上都是UIView,比如一個(gè)按鈕、一個(gè)文本標(biāo)簽、一個(gè)文本...
    Hevin_Chen閱讀 1,222評(píng)論 0 10

友情鏈接更多精彩內(nèi)容