iOS中如何暫停和繼續CALayer上的動畫

本文主要介紹瞭如何暫停和繼續CALayer的動畫. 首先來看CALayer.git

/** The base layer class. **/

@interface CALayer : NSObject <NSCoding, CAMediaTiming>
複製代碼

NSCoding比較經常使用, 就很少說了. 那這個CAMediaTiming是個什麼東西!github

CAMediaTiming

/* The CAMediaTiming protocol is implemented by layers and animations, it
 * models a hierarchical timing system, with each object describing the
 * mapping from time values in the object's parent to local time. * * Absolute time is defined as mach time converted to seconds. The * CACurrentMediaTime function is provided as a convenience for querying the * current absolute time. * * The conversion from parent time to local time has two stages: * * 1. conversion to "active local time". This includes the point at * which the object appears in the parent's timeline, and how fast it
 * plays relative to the parent.
 *
 * 2. conversion from active to "basic local time". The timing model
 * allows for objects to repeat their basic duration multiple times,
 * and optionally to play backwards before repeating. */
複製代碼

從以上介紹咱們大概瞭解到CALayer繼承了CAMediaTiming協議, 則能夠在layer與其父對象之間進行時間轉換. 即, layer上的動畫時間能夠與實際的時間進行必定的轉換. 轉換的步驟也描述地比較清楚. 那麼這個轉換有什麼意義呢?bash

再看CAMediaTiming, 包含了不少屬性. iOS中給protocol定義屬性, 其實是沒有對應的實例變量的, 只有getter/setter方法. 這一點與category相似: 給category添加屬性,實際上只會添加getter/setter方法,不會添加真正的實例變量。 由於category是在runtime決定的. 當添加實例變量的話, 類對象的內存空間就要發生變化了. 而類對象的內存空間是在編譯時期就肯定了的. 所以不能給category添加實例變量, 但屬性對應的getter/setter依然有效. protocol也是一樣的道理. 關於這一點的理解, 不知是否有不許確的地方, 歡迎一塊兒討論.app

@protocol CAMediaTiming

/* The begin time of the object, in relation to its parent object, if
 * applicable. Defaults to 0. */

@property CFTimeInterval beginTime;

/* The basic duration of the object. Defaults to 0. */

@property CFTimeInterval duration;

/* 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. */

@property float speed;

/* 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. */

@property CFTimeInterval timeOffset;

/* The repeat count of the object. May be fractional. Defaults to 0. */

@property float repeatCount;

/* The repeat duration of the object. Defaults to 0. */

@property CFTimeInterval repeatDuration;

/* When true, the object plays backwards after playing forwards. Defaults
 * to NO. */

@property BOOL autoreverses;

/* 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'. */ @property(copy) NSString *fillMode; @end 複製代碼

看到了咱們很是熟悉的duration和autoreverses, 原來是CAMediaTiming中才有的. 最初還覺得是CALayer自身的屬性... 另外幾個關鍵的屬性, beginTime, speed, timeOffset分別是什麼東西呢?ide

  • beginTime: 繼承CAMediaTiming協議的對象的起始時間, 與父對象有關係. 什麼鬼, 不是很是明瞭...
  • speed: 表示local time與parent time的比例關係, 默認爲1, 即兩者時間保持一致. 舉了個例子, 當speed爲2的時候, local time會比parent time快一倍.
  • timeOffset: 時間偏移量, 這個就更難理解了. 總之, 就是用於在local time與parent time之間進行轉換的一個什麼偏移量.

看了這些註釋, 依然不曉得具體怎麼使用CAMediaTiming及其屬性, 那麼請看下邊的實例. 經過暫停動畫和繼續動畫的兩個方法, 很是簡明地介紹了這些相關的屬性.動畫

開始動畫

[UIView animateWithDuration:2.0 animations:^{
    view1.frame = CGRectMake(self.view.frame.size.width - 100, 100, 100, 100);
} completion:^(BOOL finished) {
}];
複製代碼

暫停動畫

- (void)demosAnimationPause:(UIButton *)sender {
    // 將當前時間CACurrentMediaTime轉換爲layer上的時間, 即將parent time轉換爲local time
    CFTimeInterval pauseTime = [view1.layer convertTime:CACurrentMediaTime() fromLayer:nil];

    // 設置layer的timeOffset, 在繼續操做也會使用到
    view1.layer.timeOffset = pauseTime;

    // local time與parent time的比例爲0, 意味着local time暫停了
    view1.layer.speed = 0;
}
複製代碼

One use of this is to "pause" a layer by setting speed to zero and offset to a suitable value.ui

暫停動畫的操做實際上很是簡單.this

繼續動畫

- (void)demosAnimationContinue:(UIButton *)sender {
    // 時間轉換
    CFTimeInterval pauseTime = view1.layer.timeOffset;
    // 計算暫停時間
    CFTimeInterval timeSincePause = CACurrentMediaTime() - pauseTime;
    // 取消
    view1.layer.timeOffset = 0;
    // local time相對於parent time世界的beginTime
    view1.layer.beginTime = timeSincePause;
    // 繼續
    view1.layer.speed = 1;
}
複製代碼

那麼如何繼續執行動畫呢? 從新正確設置local time的beginTime與speed便可. 其實, 最難理解的就是local time與parent time, 及其之間的轉換關係.spa

Demo

Demo請參考: iOS-Animationcode

相關文章
相關標籤/搜索