iOS --- 如何暂停和继续CALayer上的动画

本文主要介绍了如何暂停和继续CALayer的动画. 首先来看CALayer.

/** The base layer class. **/

@interface CALayer : NSObject <NSCoding, CAMediaTiming>

NSCoding比较常用, 就不多说了. 那这个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 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;


看到了我们非常熟悉的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, 及其之间的转换关系.