CABasicAnimation animationDidStart确实调用了,但是animationDidStop从未调用过

时间:2015-10-28 19:42:40

标签: ios core-animation calayer cabasicanimation ios-animations

saw lot answers topic关于此Here is a video with complete animation and logging timeline,但无法解决我的具体情况。

{{3}}

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没有。

这怎么可能?

2 个答案:

答案 0 :(得分:2)

The animationDidStop will get called, had you waited longer. But this is, certainly, not the behavior you expected.

I gather that you would like to let the animation run for 3 seconds instead of 3000; and once completed, receive a notification from the delegate callback, which is animationDidStop.

So, instead, you would need to change:

animation.duration = 3 

and:

animation.toValue = 1

Thereby, you would see that both animationDidStart and animationDidStop will get called accordingly.

答案 1 :(得分:0)

animationDidStart和animationDidStop都没有工作。

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

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

delegate.play()

Anitmation有效,但覆盖方法无效。