当应用程序到达前台时,如何重新启动基于块的动画? [英] How can I restart my block-based animation when the application comes to the foreground?

查看:98
本文介绍了当应用程序到达前台时,如何重新启动基于块的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下基于块的动画:

I have the following block-based animation:

[UIView animateWithDuration:0.5f delay:0.0f 
                    options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut
                    animations:^{
                [view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)];
                NSLog(@"animating");
                    }completion:^(BOOL finished){
                        NSLog(@"Completed");
                    }];

当应用程序从后台返回时,调用完成块, t重启。我试图使用下面的委托方法重新启动动画:

When the app returns from being in the background, the completion block is called, and my animations don't restart. I've tried to use the following delegate method to restart the animations:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
    [[self viewController] animate];
    ......
}

同样,我已经尝试了对这些问题的答案中规定的方法:

Similarly, I've tried the approaches laid out in the answers to these questions:

当应用程序从后台恢复时,将动画恢复为停止状态

但没有任何建议对我有用。当应用程序从后台返回时,是否有另一种方法来恢复基于块的UIView动画?

but none of the suggestions there have worked for me. Is there another way to resume block-based UIView animations when an application has returned from the background?

推荐答案

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
    [UIView enableAnimations:YES];
    [[self viewController] animate];
    ......
}

动画需要removeAllAnimations并将layer.transform设置为Identity

then in the before the block animation need to removeAllAnimations and set layer.transform to Identity

hasStarted = YES;
    for(UIButton * button in goldenBreakOutButtons){
        for (UIView* view in button.subviews) {
            if (wasStarted) {
                [view.layer removeAllAnimations];
                view.layer.transform = CATransform3DIdentity;
            }
            if ([view isKindOfClass:[UIImageView class]]) {
                [UIView animateWithDuration:0.5f delay:0.0f 
                        options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat
                        animations:^ {
                            [view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)];
                            NSLog(@"animating");
                        }
                        completion:^(BOOL finished){
                            if (finished) {
                                NSLog(@"Completed");
                            }

                        }];
            }
        }
    }

这篇关于当应用程序到达前台时,如何重新启动基于块的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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