How to identify CAAnimation within the animationDidStop delegate?

后端 未结 10 2186
梦毁少年i
梦毁少年i 2020-11-28 18:26

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

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 19:03

    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()
            }
        }
    

提交回复
热议问题