CAMediaTiming / 时间协议

2016-01-23  本文已影响244人  fever105

CAMediaTiming / 时间协议

/**
当前时间2秒以后开始动画
*/
keyFrameAnim.beginTime = CACurrentMediaTime() + 2;
/**
截止到当前时间,动画已经执行了2秒,
注意,如果执行的时间大于动画时长,则表示动画已经执行过。
*/
keyFrameAnim.beginTime = CACurrentMediaTime() - 2;

 /**
  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;
    }
}

上一篇下一篇

猜你喜欢

热点阅读