ios - 当应用程序进入前台时,如何重新启动基于 block 的动画?

标签 ios cocoa-touch uiview core-animation

我有以下基于 block 的动画:

[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");
                    }];

当应用程序从后台返回时,完成 block 被调用,我的动画不会重新启动。我尝试使用以下委托(delegate)方法重新启动动画:

- (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];
    ......
}

但这并不能恢复动画。

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

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

最佳答案

一个 friend 解决了问题,需要在 View 从后台返回时在 View 上启用动画

- (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];
    ......
}

然后在 block 动画之前需要移除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");
                            }

                        }];
            }
        }
    }

关于ios - 当应用程序进入前台时,如何重新启动基于 block 的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7670032/

相关文章:

ios - EarlGrey - 如何检查屏幕上是否显示多个对象

iphone - 如何防止 UITextfield 字体大小在您键入时变小

iPhone - 调试 EXC_BAD_ACCESS 崩溃

ios - 使用随机坐标重复 UIAnimation

ios - iOS 和 Objective C 中的循环过渡( mask )

ios - 使用日期选择器的本地 UNMutableNotificationContent 触发日期

ios - Xcode 8 GM 破坏了我的 UI

ios - 如何针对特定设备查看iOS应用商店收据?

objective-c - 了解示例代码中多次提到的委托(delegate)协议(protocol)

ios - -[UIView setContentSize :]: unrecognized selector sent to instance 0x16d3be60