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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,829评论 1 331
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,603评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,846评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,600评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,780评论 3 272
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,695评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,136评论 2 293
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,862评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,453评论 0 229
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,942评论 2 233
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,347评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,790评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,293评论 3 221
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,839评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,448评论 0 181
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,564评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,623评论 2 249

推荐阅读更多精彩内容