本文主要介紹了如何暫停和繼續(xù)CALayer的動(dòng)畫(huà). 首先來(lái)看CALayer.
/** The base layer class. **/
@interface CALayer : NSObject <NSCoding, CAMediaTiming>
NSCoding比較常用, 就不多說(shuō)了. 那這個(gè)CAMediaTiming是個(gè)什么東西!
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協(xié)議, 則可以在layer與其父對(duì)象之間進(jìn)行時(shí)間轉(zhuǎn)換.
即, layer上的動(dòng)畫(huà)時(shí)間可以與實(shí)際的時(shí)間進(jìn)行一定的轉(zhuǎn)換. 轉(zhuǎn)換的步驟也描述地比較清楚.
那么這個(gè)轉(zhuǎn)換有什么意義呢?
再看CAMediaTiming, 包含了很多屬性. iOS中給protocol定義屬性, 實(shí)際上是沒(méi)有對(duì)應(yīng)的實(shí)例變量的, 只有g(shù)etter/setter方法.
這一點(diǎn)與category類似: 給category添加屬性柱搜,實(shí)際上只會(huì)添加getter/setter方法,不會(huì)添加真正的實(shí)例變量剥险。
因?yàn)閏ategory是在runtime決定的. 當(dāng)添加實(shí)例變量的話, 類對(duì)象的內(nèi)存空間就要發(fā)生變化了. 而類對(duì)象的內(nèi)存空間是在編譯時(shí)期就確定了的.
因此不能給category添加實(shí)例變量, 但屬性對(duì)應(yīng)的getter/setter依然有效.
protocol也是同樣的道理.
關(guān)于這一點(diǎn)的理解, 不知是否有不準(zhǔn)確的地方, 歡迎一起討論.
@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, 原來(lái)是CAMediaTiming中才有的. 最初還以為是CALayer自身的屬性...
另外幾個(gè)關(guān)鍵的屬性, beginTime, speed, timeOffset分別是什么東西呢?
- beginTime: 繼承CAMediaTiming協(xié)議的對(duì)象的起始時(shí)間, 與父對(duì)象有關(guān)系. 什么鬼, 不是非常明了...
- speed: 表示local time與parent time的比例關(guān)系, 默認(rèn)為1, 即二者時(shí)間保持一致. 舉了個(gè)例子, 當(dāng)speed為2的時(shí)候, local time會(huì)比parent time快一倍.
- timeOffset: 時(shí)間偏移量, 這個(gè)就更難理解了. 總之, 就是用于在local time與parent time之間進(jìn)行轉(zhuǎn)換的一個(gè)什么偏移量.
看了這些注釋, 依然不曉得具體怎么使用CAMediaTiming及其屬性, 那么請(qǐng)看下邊的實(shí)例.
通過(guò)暫停動(dòng)畫(huà)和繼續(xù)動(dòng)畫(huà)的兩個(gè)方法, 非常簡(jiǎn)明地介紹了這些相關(guān)的屬性.
開(kāi)始動(dòng)畫(huà)
[UIView animateWithDuration:2.0 animations:^{
view1.frame = CGRectMake(self.view.frame.size.width - 100, 100, 100, 100);
} completion:^(BOOL finished) {
}];
暫停動(dòng)畫(huà)
- (void)demosAnimationPause:(UIButton *)sender {
// 將當(dāng)前時(shí)間CACurrentMediaTime轉(zhuǎn)換為layer上的時(shí)間, 即將parent time轉(zhuǎn)換為local time
CFTimeInterval pauseTime = [view1.layer convertTime:CACurrentMediaTime() fromLayer:nil];
// 設(shè)置layer的timeOffset, 在繼續(xù)操作也會(huì)使用到
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.
暫停動(dòng)畫(huà)的操作實(shí)際上非常簡(jiǎn)單.
繼續(xù)動(dòng)畫(huà)
- (void)demosAnimationContinue:(UIButton *)sender {
// 時(shí)間轉(zhuǎn)換
CFTimeInterval pauseTime = view1.layer.timeOffset;
// 計(jì)算暫停時(shí)間
CFTimeInterval timeSincePause = CACurrentMediaTime() - pauseTime;
// 取消
view1.layer.timeOffset = 0;
// local time相對(duì)于parent time世界的beginTime
view1.layer.beginTime = timeSincePause;
// 繼續(xù)
view1.layer.speed = 1;
}
那么如何繼續(xù)執(zhí)行動(dòng)畫(huà)呢? 重新正確設(shè)置local time的beginTime與speed即可.
其實(shí), 最難理解的就是local time與parent time, 及其之間的轉(zhuǎn)換關(guān)系.
Demo
Demo請(qǐng)參考:
iOS-Animation