Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS, Restarting animation when coming out of the background

when my app comes out of the background the animation has stopped. which is normal. but i want to restart my animation from the current state. how do i do that without my snapping all over the place.

[UIView animateWithDuration:60 delay:0 options:(UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) animations:^{     [bg setFrame:CGRectMake(0, 0, 1378, 1005)]; } completion:nil]; 

i tried putting a set frame in front of the animation but that just makes it snap.

[bg setFrame:CGRectMake(0, 0, 1378, 1005)]; 

any ideas?

like image 433
Stultus Avatar asked May 18 '11 09:05

Stultus


People also ask

Why does Pokémon Go keep restarting when put in the background?

One thing to check is that for Pokémon Go Settings — General — Background App refresh is on. If it’s not the app will always restart when it’s put in the background. Eureka! Been trying to fix this for so long, thanks! IOS11 right? It more aggressively shuts down apps in the background if there isn’t enough memory. You have an older iPhone?

Why does Pokemon Go keep restarting on my iPhone 6?

This is on an iPhone 6 with 16 GB of memory. One thing to check is that for Pokémon Go Settings — General — Background App refresh is on. If it’s not the app will always restart when it’s put in the background. Eureka! Been trying to fix this for so long, thanks! IOS11 right?

Why won't my phone turn off Pokemon Go in the background?

Most of the time if only one other app open (that's the tip), phone will not shut down Pokemon Go when in background. Sometimes it does anyway. This happened to me randomly after an update. Simply minimising it and going straight back on would make it restart. In the end reinstalling the app fixed the issue.


2 Answers

You can add an observer in your class for UIApplicationWillEnterForegroundNotification:

- (void)addNotifications {      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];  }  - (void)applicationWillEnterForeground {      [self animate];  }  - (void)animate {      [bg setFrame:CGRectMake(0, 0, 0, 0)];      [UIView animateWithDuration:60 delay:0 options:(UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) animations:^{         [bg setFrame:CGRectMake(0, 0, 1378, 1005)];     } completion:nil]; } 

It is important to set the begin state of the animation (and don't forget to remove the notification observer)

like image 165
Ramiro Avatar answered Sep 20 '22 21:09

Ramiro


well the answer of @dany_23 could work.

But I came across an other method that works just fine if you don't need to resume your animation but restart your animation, without the view or layer snapping when you reactivate the app.

in the

- (void)applicationWillResignActive:(UIApplication *)application 

you call a method in your viewcontroller which implements the following code.

[view.layer removeAllAnimations];  // this following CGRect is the point where your view originally started  [bg setFrame:CGRectMake(0, 0, 1378, 1005)];  

and in the

- (void)applicationDidBecomeActive:(UIApplication *)application 

you call a method in your viewcontroller that just starts the animation. something like

[UIView animateWithDuration:60 delay:0 options:(UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) animations:^{       [bg setFrame:CGRectMake(0, 0, 1378, 1005)]; } completion:nil]; 

Hope this helps, Thanks to all who replied.

like image 44
Jonathan Avatar answered Sep 21 '22 21:09

Jonathan