CAAnimationDelegate
Methods your app can implement to respond when animations start and stop.
Declaration
protocol CAAnimationDelegate : NSObjectProtocolOverview
You can use an animation delegate to execute additional logic when an animation starts or ends. For example, you may want to remove a layer from its parent once a fade out animation has completed.
The following example shows code taken from a class that implements CAAnimationDelegate and has had a layer, named sublayer, added to its layer. The fadeOut function animates the opacity of sublayer and, once the animation has completed, animationDidStop(_:finished:) removes it from its superlayer.
func fadeOut() {
let fadeOutAnimation = CABasicAnimation()
fadeOutAnimation.keyPath = "opacity"
fadeOutAnimation.fromValue = 1
fadeOutAnimation.toValue = 0
fadeOutAnimation.duration = 0.25
fadeOutAnimation.delegate = self
sublayer.add(fadeOutAnimation,
forKey: "fade")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
sublayer.removeFromSuperlayer()
}