2

I saw a lot of answers about this topic, but can not solve my particular case.

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

The problem is animationDidStart called, but animationDidStop not.

How this is possible?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
George
  • 643
  • 9
  • 23

2 Answers2

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
0

Neither animationDidStart nor animationDidStop worked.

I try to work this lines in a view controller :

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

delegate.play()

Anitmation worked but override methods didn't work.

gkhanacer
  • 575
  • 7
  • 25