iOS动画在背景/前景转换后停止

iOS动画在背景/前景转换后停止,ios,objective-c,animation,Ios,Objective C,Animation,我正在写一个页面,检查设备是否已链接到系统。 页面每2秒发送请求6次,在此期间(可能长达10秒),页面显示动画加载循环 问题是:当我按下home(主页)按钮并立即重新打开应用程序时,动画停止 以下是我所知道的: 实现动画的两种方法: CABasicAnimation: CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotatio

我正在写一个页面,检查设备是否已链接到系统。 页面每2秒发送请求6次,在此期间(可能长达10秒),页面显示动画加载循环

问题是:当我按下home(主页)按钮并立即重新打开应用程序时,动画停止

以下是我所知道的:

实现动画的两种方法:
  • CABasicAnimation

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = @(2 * M_PI);
    rotationAnimation.duration = 1.5;
    rotationAnimation.cumulative = YES;
    [loadingCircle.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
  • UIView animateWithDuration…

    - (void)rotateView:(UIView *)view {
        [UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            view.transform = CGAffineTransformRotate(view.transform, M_PI);
        } completion:^(BOOL finished) {
            if (finished) {
                [self rotateView:view];
            }
        }];
    }
    ...
    - viewDidLoad {
        ...
        [self rotateView:loadingCircle];
    }
    
  • 我知道在第一个实现中,设置
    rotationAnimation.removedOnCompletion=NO将恢复动画


    我的问题是:当我使用第二个实现时,实现相同效果的等效方法是什么。

    使用UIView animateWithDuration实际上应用了与视图的底层
    属性相同的效果,因此可以像这样暂停和恢复

    func pauseLayer(layer: CALayer) {
        let pausedTime: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        layer.speed = 0.0
        layer.timeOffset = pausedTime
    }
    
    func resumeLayer(layer: CALayer) {
        let pausedTime: CFTimeInterval = layer.timeOffset
        layer.speed = 1.0
        layer.timeOffset = 0.0
        layer.beginTime = 0.0
        let timeSincePause: CFTimeInterval = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime
        layer.beginTime = timeSincePause
    }
    
    对不起,你必须自己把它翻译成objective-c。最初的解决方案值得称赞


    如果您希望将代码保留在本地而不是放入应用程序代理,则可以通过
    NSNotificationCenter
    订阅
    UIApplicationIdentinterBackgroundNotification
    通知。有一个匹配的
    UIApplicationWillEnterForegroundNotification
    通知,您可以用来在回来时重新启动动画。

    在appdelegate Classic中调用ApplicationIDBecMeactive中的动画方法我通过在我的本地类中订阅通知来完成。谢谢。谢谢,我尝试过通知方法,效果很好。:]