How can I restart my block-based animation when the application comes to the foreground?

霸气de小男生 提交于 2019-12-03 14:46:27
CStreel

A friend figured out the problem, needed to enableAnimations on the view when it returns from the background

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

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

                        }];
            }
        }
    }

Please try to subscribe/unsubscribe in ViewController to
standart(UIApplicationWillEnterForegroundNotification) notification
from 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"];
        }
    }

 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!