女婴被遗弃超市,gp328,奇圣胶囊
camediatiming ( 时间协议)详解
有一种通过caanimation实现的协议叫做camediatiming,也就是cabasicanimation和cakeyframeanimation的基类(指caanimation)。像duration,begintime和repeatcount这些时间相关的属性都在这个类中。大体而言,协议中定义了8个属性,这些属性通过一些方式结合在一起,准确的控制着时间。文档中每个属性只有几句话,所以很有可能在看这篇文章之前你都已经读过了,但是我觉得使用可视化的图形能更好的解释时间。
可视化的camediatiming
为了显示相关属性的不同时间,无论是他们自己还是混合状态,我都会动态的将橙色变为蓝色。下面的块状显示了从开始到结束的动画过程,时间线上每一个标志代表一秒钟。你可以看到时间线上的任意一点,当前颜色即表示动画中的当前时间。比如,duration像下面一样可视。
我们都知道,calayer和caanimation都实现了camediatiming 协议,因此在core animation中,理解camediatiming协议中的属性是非常必要的,但是苹果的文档中对于各个属性描述太简单,对初学者容易理解,这篇文章主要帮助理解camediatiming协议中各个属性的含义。
camediatiming protocol提供了8个属性,下面将分别讲解。
camediatiming / 时间协议
/** 当前时间2秒以后开始动画 */ keyframeanim.begintime = cacurrentmediatime() + 2; /** 截止到当前时间,动画已经执行了2秒, 注意,如果执行的时间大于动画时长,则表示动画已经执行过。 */ keyframeanim.begintime = cacurrentmediatime() - 2;
7.timeoffset,时间轴偏移量。将时间轴移动至偏移位置,再执行整个动画时长。假设动画时长3秒,偏移量为8,则开始位置为8 % 3 = 2,再执行3秒,即在整个时长的1/ 3处结束。
8.cacurrentmediatime,返回系统当前的绝对时间(从本次开机开始),单位秒。
/** the receiver does not appear until it begins and is removed from the presentation when it is completed. */ kcafillmoderemoved; // (默认)动画模型的呈现效果直至开始时才显示,并在动画结束后移除。 /** the receiver does not appear until it begins but remains visible in its final state when it is completed. */ kcafillmodeforwards; // 动画模型的呈现效果直至开始时才显示,但在动画结束后仍然显示最后的状态。 /** the receiver appears in its initial state before it begins but is removed from the presentation when it is completed. */ kcafillmodebackwards; // 动画开始之前,动画模型显示其初始呈现效果,但在动画结束后移除。 /** the receiver appears in its initial state before it begins and remains visible in its final state when it is completed. */ kcafillmodeboth; // 动画开始之前,动画模型显示其初始呈现效果,并且在动画结束后仍然显示最后的状态。
暂停/继续动画demo
- (ibaction)pausebtnclicked:(id)sender { /** 判断当前图层对象是否有针对postion属性的动画效果 */ if ([self.layer.presentationlayer animationforkey:@"position"]) { // 通过绝对时间获取图层的本地时间 cftimeinterval localtime = [self.layer converttime:cacurrentmediatime() fromlayer:nil]; /** 将图层的时间流逝速度设置为0,以暂停动画 */ self.layer.speed = 0; // 设置图层的时间轴偏移量,为继续动画做准备 self.layer.timeoffset = localtime; } } - (ibaction)continuebtnclicked:(id)sender { /** 判断当前图层对象是否有针对postion属性的动画效果 */ if ([self.layer.presentationlayer animationforkey:@"position"]) { // 获取上次暂停时的时间轴偏移量 cftimeinterval timeoffset = self.layer.timeoffset; // 重置时间轴偏移量 self.layer.timeoffset = 0; // 速度还原为1 self.layer.speed = 1; // 重置开始时间 #warning 此处严重不理解。 self.layer.begintime = 0; // 计算暂停时间和当前时间的差值 cftimeinterval localtime = [self.layer converttime:cacurrentmediatime() fromlayer:nil]; cftimeinterval timesincepause = localtime - timeoffset; // 从上一次暂停处开始 self.layer.begintime = timesincepause; } }
感谢阅读,希望能帮助到大家,谢谢大对本站的支持!
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
iOS 使用UITextField自定义搜索框 实现用户输入完之后“实时搜索”功能
网友评论