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