Chapter 1: Challenge 3 Animate the clouds

Hey guys! In this challenge everything is working perfect at first. The clouds were running good. But if I clicked home button and then came back. The positions of all the clouds were reset and started animation from the left side(right picture).

01

I tested it. It seems if I clicked home button and go back to home page, the animation function: animateCloud(){…} will automatically complete. So all the cloud’s frame are reset in the completion block.

I’m wondering if there any ways to keep the cloud animation running when I click home button? Thank you!

update: Core animation stops working after… | Apple Developer Forums
it says: “When your application goes into the background, all animations are removed from all layers. You will have to re-establish these animations each time you come to the foreground.”

1 Like

When the app goes to background, the animations are stopped, with a completion. They aren’t actually removed, as you can tell because they start up again when the app goes to foreground. There is no way to “keep them going” when the app is in background.

To make the animations seem to continue, you would need to act on notifications of the app going to background, and coming back to foreground. You can do that in AppDelegate, or you can register for the notifications in the view controller. Then capture the origin.x values of the clouds when the app goes to background, and restore them when it goes to foreground.

Thank you very much!

This topic was automatically closed after 166 days. New replies are no longer allowed.