Test whether a UIView is in the middle of animation Test whether a UIView is in the middle of animation ios ios

Test whether a UIView is in the middle of animation


A UIView has a layer (CALayer). You can send animationKeys to it, which will give you an array of keys which identify the animations attached to the layer. I suppose that if there are any entries, the animation(s) are running. If you want to dig even deeper have a look at the CAMediaTiming protocol which CALayer adopts. It does some more information on the current animation.

Important: If you add an animation with a nil key ([layer addAnimation:animation forKey:nil]), animationKeys returns nil.


iOS 9+ method, works even when layer.animationKeys contains no keys:

let isInTheMiddleOfAnimation = UIView.inheritedAnimationDuration > 0

From the docs:

This method only returns a non-zero value if called within a UIView animation block.


Animations are attached in fact to the underlying Core Animation CALayer class

So I think you can just check myView.layer.animationKeys