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

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

/** The base layer class. **/

@interface CALayer : NSObject <NSCoding, CAMediaTiming>

NSCoding比較常用, 就不多說了. 那這個CAMediaTiming是個什么東西!

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上的動畫時間可以與實際的時間進行一定的轉換. 轉換的步驟也描述地比較清楚.
那么這個轉換有什么意義呢?

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

@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分別是什么東西呢?

  • 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.

暫停動畫的操作實際上非常簡單.

繼續動畫

- (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, 及其之間的轉換關系.

Demo

Demo請參考:
iOS-Animation

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容