Iphone: restart loop animation after viewing hidden, then reappear?

My application has a tab bar with two different views. On the first tab, its view has an animation with a continuous loop.

When I click on the second tab, go back to the first, the animation has stopped. I know that I can start it again in the viewWillAppear: method, but the problem is bigger. In particular, the animation will also stop if the application enters the background state and then returns to the foreground. In this case, viewWillAppear is not called in the foreground, so the viewWillAppear method will do nothing.

What is the best way to deal with this situation?

Thank.

+5
source share
2 answers

To support encapsulation, you don’t want yours to AppDelegateknow which views need to resume animations. But you can have a view that contains the animation register for the corresponding notification (for example, in the view method init) and restart the animation yourself.

[[NSNotificationCenter defaultCenter] 
   addObserver:self 
      selector:@selector(startAnimation) 
          name:UIApplicationWillEnterForegroundNotification  
        object:nil];

... and don't forget to unregister from the notification center using the method dealloc.

+4
source

You can configure the animation to continue in the applicationWillEnterForeground method from AppDelegate. If you have a link to the first table view controller in AppDelegate, just call the viewWillAppear view view method from AppDelegate.

0
source

All Articles