CABasicAnimation animationDidStart 确实被调用了,但是 animationDidStop 从来没有被调用过

CABasicAnimation animationDidStart did called, but animationDidStop never was

saw a lot of answers about this topic,但无法解决我的特殊情况。

Here is a video with complete animation and logging timeline

class AnimationDelegate: UIView {

    deinit {
        print("AnimationDelegate deinitialized")
    }

    override func animationDidStart(anim: CAAnimation) {
        print("animationDidStart")
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        print("animationDidStop")
    }

    func play() {
        let line_1 = CAShapeLayer()
        ...

        let animation = CABasicAnimation()
        animation.delegate = self
        animation.duration = 3000
        animation.repeatCount = 1
        animation.keyPath = "strokeEnd"
        animation.fromValue = 0
        animation.toValue = 360

        layer.addSublayer(line_1)
        line_1.addAnimation(animation, forKey: "StrokeAnimation")

    }
}

let delegate = AnimationDelegate(frame: container.bounds)
container.addSubview(delegate)

delegate.play()

问题是 animationDidStart 调用了,但 animationDidStop 没有调用。

这怎么可能?

animationDidStop 会被呼叫,如果你等得更久的话。但这肯定不是您期望的行为。

我了解到您想让动画 运行 持续 3 秒而不是 3000 秒;一旦完成,就会收到来自委托回调的通知,即 animationDidStop.

因此,您需要更改:

animation.duration = 3 

和:

animation.toValue = 1

因此,您会看到 animationDidStartanimationDidStop 都会相应地被调用。

animationDidStart 和 animationDidStop 都不起作用。

我尝试在视图控制器中处理这些行:

let delegate = AnimationDelegate(frame: container.bounds)
container.addSubview(delegate)

delegate.play()

动画有效但覆盖方法无效。