为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法暂停CABasicAnimation?

有没有办法暂停CABasicAnimation?

繁星coding 2019-11-08 12:51:52
我有一个基本的iPhone旋转动画。有什么方法可以“暂停”动画,以便维持视图的位置?我想这样做的一种方法是使动画“完成”,而不是对其调用“删除”,我该怎么做?CABasicAnimation* rotationAnimation;rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2];rotationAnimation.duration = 100;rotationAnimation.cumulative = YES;rotationAnimation.repeatCount = HUGE_VALF;rotationAnimation.removedOnCompletion = NO;rotationAnimation.fillMode = kCAFillModeForwards;[myView.layer addAnimation:rotationAn
查看完整描述

3 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

最近出现的Apple技术说明QA1673描述了如何暂停/恢复图层的动画。


暂停和恢复动画列表如下:


-(void)pauseLayer:(CALayer*)layer

{

    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];

    layer.speed = 0.0;

    layer.timeOffset = pausedTime;

}


-(void)resumeLayer:(CALayer*)layer

{

    CFTimeInterval pausedTime = [layer timeOffset];

    layer.speed = 1.0;

    layer.timeOffset = 0.0;

    layer.beginTime = 0.0;

    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;

    layer.beginTime = timeSincePause;

}

编辑: iOS 10引入了新的API-UIViewPropertyAnimator,该API可以更交互地处理动画,例如,它使暂停和恢复动画或“搜索”到特定进度值变得容易。


查看完整回答
反对 回复 2019-11-08
?
HUH函数

TA贡献1836条经验 获得超4个赞

回答Swift 3:


积分@Vladimir


码:


 func pauseAnimation(){

  let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)

  layer.speed = 0.0

  layer.timeOffset = pausedTime

}


func resumeAnimation(){

  let pausedTime = layer.timeOffset

  layer.speed = 1.0

  layer.timeOffset = 0.0

  layer.beginTime = 0.0

  let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime

  layer.beginTime = timeSincePause

}


查看完整回答
反对 回复 2019-11-08
?
心有法竹

TA贡献1866条经验 获得超5个赞

您可以使用计时器或处理动画委托方法:


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

这是我的代码:


// ...

[self startAnimation];

// ...


- (void)startAnimation {

CABasicAnimation* rotationAnimation;

rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

rotationAnimation.fromValue = [NSNumber numberWithFloat:0];

rotationAnimation.toValue = [NSNumber numberWithFloat: M_2_PI];

rotationAnimation.duration = 1.0;

rotationAnimation.cumulative = YES;

// rotationAnimation.repeatCount = 0; // <- if repeatCount set to infinite, we'll not receive the animationDidStop notification when the animation is repeating

rotationAnimation.removedOnCompletion = NO;

rotationAnimation.fillMode = kCAFillModeForwards;

rotationAnimation.delegate = self; // <- hanlde the animationDidStop method

[myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];


}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

if (shouldContinueAnimation) // <- set a flag to start/stop the animation

    [self startAnimation];

}

希望它能对您有所帮助。


查看完整回答
反对 回复 2019-11-08
  • 3 回答
  • 0 关注
  • 2010 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信