WishIHadThreeGuns
WishIHadThreeGuns

Reputation: 1469

Remove CoreGraphics Animation

How can I remove an animation using animationDidStop?

I apply the animation

let animationTwo = CABasicAnimation(keyPath: "opacity")
animationTwo.delegate = self
animationTwo.fromValue = 0.0
animationTwo.toValue = 1.0
animationTwo.duration = 1.0
animationTwo.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
myView.layer.add(animationTwo, forKey: "fade")

And then try to remove it

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    if anim == myView.layer.animation(forKey: "fade") {
        myView.layer.removeAnimation(forKey: "fade")
    }
}

However anim == myView.layer.animation(forKey: "fade") never resolves to true.

How can I remove the animation when it is complete?

Upvotes: 0

Views: 78

Answers (1)

May Rest in Peace
May Rest in Peace

Reputation: 2207

CAAnimation has a property called isRemovedOnCompletion which defaults to true

This means your animation would be removed from render tree when it is finished. I guess that's why anim == myView.layer.animation(forKey: "fade") is not resolving to true because it has already been removed.

Upvotes: 4

Related Questions