I had a problem where I had a series of overlapping CATransition / CAAnimation sequences, all of which I needed to perform custom operations when the animations stopped, but
Xcode 9 Swift 4.0
You can use Key Values to relate an animation you added to the animation returned in animationDidStop delegate method.
Declare a dictionary to contain all active animations and related completions:
var animationId: Int = 1
var animating: [Int : () -> Void] = [:]
When you add your animation, set a key for it:
moveAndResizeAnimation.setValue(animationId, forKey: "CompletionId")
animating[animationId] = {
print("completion of moveAndResize animation")
}
animationId += 1
In animationDidStop, the magic happens:
let animObject = anim as NSObject
if let keyValue = animObject.value(forKey: "CompletionId") as? Int {
if let completion = animating.removeValue(forKey: keyValue) {
completion()
}
}