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

我有以下基于块的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"); }]; 

当应用程序从后台返回时,将调用完成块,并且我的animation不会重新启动。 我试图使用下面的委托方法来重新启动animation:

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

但是这并没有工作来恢复animation。

同样,我已经尝试了解决这些问题的方法:

  • iOS,退出后台时重新启动animation

  • 当应用程序从后台恢复时,在animation停止的位置恢复

但没有任何build议为我工作。 当应用程序从后台返回时,是否有另一种方法来恢复基于块的UIViewanimation?

一个朋友想出了这个问题,需要在视图返回时从后台启用animation

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

那么在块animation之前需要删除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"); } }]; } } } 

请尝试在ViewController中订阅/取消订阅
标准( UIApplicationWillEnterForegroundNotification )通知
来自NSNotificationCenter

 -(void) loadView { ..... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(restartAnimation) name:UIApplicationWillEnterForegroundNotification object:nil]; .... } - (void) viewDidUnload { ... [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; .... } -(void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } - (void) restartAnimation { if(self.logoImageView) { [logoImageView.layer removeAllAnimations]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.fromValue = [NSNumber numberWithFloat:1.0]; animation.toValue = [NSNumber numberWithFloat:0.6]; animation.duration = 1.5f; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; animation.autoreverses = YES; animation.repeatCount = HUGE_VALF; [[logoImageView layer] addAnimation:animation forKey:@"hidden"]; } }