Search code examples
objective-ccore-animation

Layer animations removed when switching apps


(iOS 5.0)

I have an CAKeyframeAnimation added to a view's layer with an infinite repeat count (a cursor blinking). However, when switching apps and coming back, the animation gets removed from the layer (or possibly the entire layer is replaced, I'm not sure).

I tried re-adding them on viewDidLoad and viewWillAppear, but neither is called when switching apps. I did find there is a applicationWillEnterForeground: method in UIApplicationDelegate. I'm planning to use this but I have two questions:

  • Are there any other circumstances that trigger the animations or layer to be unloaded? Is there a more appropriate event to listen to?
  • What is the best practice (in an design pattern sense) to respond to these events (e.g. using target action)?

Solution

  • When switching apps or view controllers, the system will let the animations on the layer stop (complete), so you can set removedOnCompletion to NO (the default is YES), and it will work.