Search code examples
iphoneiosanimationcalayercaanimation

How to remove a layer when its animation completes?


I am making an iOS App. I have several CALayer objects that eventually will be deleted by a (shrinking) animation. When the animation is completed, and animationDidStop:finished is invoked, I would like to remove the CALayer object from the super view and delete it.

  1. But how can I get the CALayer object in animationDidStop:finished? I would have guessed that the CAanimation-object had a pointer to the layer, but I can't find it in the doc.
  2. Is there a better way to handle the issue? (Actually, I have several animation objects added to the same layer, and, ideally, I would like to remove the layer only when the last animation completes)

Solution

  • When you create the animation and set the delegate, just pass the CALayer you want to remove with your animation.

    As for removing all the animations, you have two options:

    1. You can check your CALayer's animationKeys for any existing animations.
    2. You can use a CAAnimationGroup and group all your animations together.