有没有办法暂停CABasicAnimation? [英] Is there a way to pause a CABasicAnimation?

查看:816
本文介绍了有没有办法暂停CABasicAnimation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone的基本旋转动画。有什么方法可以暂停动画,以便保持视图的位置?我想这样做的一种方法是让动画完成而不是在其上调用删除,我该怎么做?

I have a basic spinning animation of the iPhone. Is there any way that I can "pause" the animation so that the position of the view will be maintained? I guess one way of doing this would be to cause the animation to "complete" instead of calling "remove" on it, how would I do that?

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:rotationAnimation forKey:@"rotationAnimation"];


推荐答案

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

Recently appeared Apple's technical note QA1673 describes how to pause/resume layer's animation.

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

Pause and resume animations listing is below:

-(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允许以交互方式处理动画,例如,它可以轻松地暂停和恢复动画或寻找某个特定的进度值。

iOS 10 introduced new API - UIViewPropertyAnimator that allows to handle animations more interactively, for example it makes easy to pause and resume animation or 'seek' it to some particular progress value.

这篇关于有没有办法暂停CABasicAnimation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆