【问题标题】:Is there a way to call a method when a CAKeyframeAnimation finishes animating?有没有办法在 CAKeyframeAnimation 完成动画时调用方法?
【发布时间】:2015-11-24 18:51:19
【问题描述】:

我正在使用 Swift,我需要在 CAKeyframeAnimation 完成后做一些事情。

如果这是UIView,我通常只会使用UIView.animateWithDuration,然后在completionHandler 中实现这些内容,如下所示:

    UIView.animateWithDuration(duration, delay: delay, options: [],
        animations: { },
        completion: {
            (animationFinished: Bool) in

            if animationFinished {
            // DO SOME STUFF IN HERE ONCE THE ANIMATION FINISHES
            }
    })

使用CAKeyframeAnimation 的等效方法是什么?

【问题讨论】:

    标签: ios swift nsobject caanimation cakeyframeanimation


    【解决方案1】:

    我想我想出了如何做到这一点。

    这里有一些示例代码可以粘贴到 Xcode 的 Playground 中:

    import XCPlayground
    
    class BallAnimation: NSObject { // <- STEP 1 - INHERIT FROM NSOBJECT
      func doMyAnimation() {
        // Make a test background.
        let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
        XCPlaygroundPage.currentPage.liveView = backgroundView
    
        // Create the object that will be animated and add it to the background.
        let ballImage = UIImage(named: "ball")
        let ballView = UIImageView(image: ballImage)
        backgroundView.addSubview(ballView)
    
        // Create the path the object will follow.
        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x: 200, y: 200))
        path.addLineToPoint(CGPoint(x: 200, y:100))
    
        // Create the animation that will use the path we just created.
        let animation = CAKeyframeAnimation(keyPath: "position")
        animation.path = path.CGPath
        animation.duration = 2.0
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards
        animation.delegate = self // <- STEP 2 - SET THE DELEGATE
    
        // This should start the animation
        ballView.layer.addAnimation(animation, forKey: "animate ball")
      }
    
      // STEP 3 - OVERRIDE THIS METHOD
      override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        // When the animation stops it should call this method (make sure the delegate was set above).
        print("Animation did stop.")
      }
    }
    
    // Create an instance of the class and call the method that starts the animation.
    let ballAnimation = BallAnimation()
    ballAnimation.doMyAnimation()
    

    解释

    CAAnimation 类将以下方法添加到NSObject

    func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
    

    因此,如果一个类继承自 NSObject,则可以将其设置为 CAKeyframeAnimation 委托,并且可以在动画结束时覆盖 animationDidStop 以执行其他任务。

    【讨论】:

      【解决方案2】:

      我在 XCode 12 中做了类似的事情:

      class ViewController: UIViewController, CAAnimationDelegate {
      
      func creatAnimation() {
      let animation = CAKeyframeAnimation()
      // some code configuring animation
      animation.delegate = self
      }
      
      func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
              print("done")
              
       }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-08
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多