Search code examples
iosobjective-cuiviewcore-animation

CATransaction complete block success or failure


I am working with Core Animation using CATransaction. I am using setCompletionBlock in order to capture when the animation is complete so that I can do stuff with the data in the view controller, but I want the animation to be interruptible. Eg. when I call

[self.layer removeAllAnimations]

the animation should stop but the setCompletionBlock should also KNOW if the animation succeeded or failed.

With UIView animation, this is possible since there is a finished variable passed in the completion block and with CAAnimationGroup this is also possible with a finished variable passed to the delegate. How do I accomplish the same with a CATransaction?


Solution

  • Using key-value coding, you can pass any variable you like into the current transaction where it can be picked up by the completion block later. CATransaction, CAAnimation, CALayer, they all accept arbitrary key-value pairs which you can create and use to your heart's content.

    https://developer.apple.com/library/ios/documentation/graphicsimaging/Reference/CATransaction_class/Introduction/Introduction.html#//apple_ref/occ/clm/CATransaction/setValue:forKey: