Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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 active state; here you can undo many of the changes made on entering the background.
     */
    [[self viewController] animate];
    ......
}

but this hasn't worked to restore the animations.

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

  • iOS, Restarting animation when coming out of the background

  • Restoring animation where it left off when app resumes from background

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?

like image 274
CStreel Avatar asked Oct 06 '11 03:10

CStreel


People also ask

Why doesn't the CSS animation work when JavaScript is blocked?

That's because the CSS animation will only trigger when the square-animation class is added, not removed. Even though the animation shows the element entering, the element is visible before the animation class is added. When the page first loads, we want the element to be visible for the user even if the JavaScript is blocked or fails.

How to control when the animation is played?

We can control when the animation is played by adding the animation properties to a separate class than the class used to style the element. // etc... When we add the animation class to the square, the animation will play.

Is there a way to restart a CSS animation?

With CSS animations (ala @keyframes) it’s not as easy as you might think to “restart” it. Let’s say you set it to run once: You have that run on, say, your logo: Then for some reason you want to have that animation run again, perhaps on a click. You might think CSS provides a way to kick it off again, but as far as I know it doesn’t.

Does the default animation change when you press any button?

Well, it depends. For example, when i start the game, my default animation is set to be idle. Then whenever i press any button, my animation transitions to another animation, when i stopped pressing any button it transitions back to my idle animation.


2 Answers

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

                        }];
            }
        }
    }
like image 159
CStreel Avatar answered Nov 15 '22 00:11

CStreel


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

 
like image 42
ChapayStack Avatar answered Nov 14 '22 23:11

ChapayStack