当应用程序到达前台时,如何重新启动基于块的动画?

CSt*_*eel 6 cocoa-touch core-animation uiview ios

我有以下基于块的动画:

[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");
                    }];
Run Code Online (Sandbox Code Playgroud)

当应用程序从后台返回时,将调用完成块,并且我的动画不会重新启动.我尝试使用以下委托方法重新启动动画:

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

但这并没有恢复动画.

同样,我尝试了这些问题的答案中列出的方法:

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

CSt*_*eel 5

一位朋友发现了问题,需要在视图从后台返回时启用视图上的动画

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

然后在块动画之前需要删除AllAnimations并将layer.transform设置为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");
                            }

                        }];
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 你究竟在哪里找到`[UIView enableAnimations:YES]`?我在任何地方都没有看到这种方法:( (2认同)
  • 有点晚了,但试试[UIView setAnimationsEnabled:YES]; (2认同)