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

查看:75
本文介绍了有没有办法暂停 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"];

推荐答案

最近出现的苹果技术说明 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天全站免登陆