ios - CABasicAnimationanimationDidStart确实被调用,但animationDidStop从未被调用

标签 ios swift core-animation calayer ios-animations

saw一个lotanswers关于这个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 秒;完成后,从委托(delegate)回调接收通知,即 animationDidStop

因此,您需要更改:

animation.duration = 3 

和:

animation.toValue = 1

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

关于ios - CABasicAnimationanimationDidStart确实被调用,但animationDidStop从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33400206/

相关文章:

ios - 动画 block 中 UIView 的旋转和设置中心有时不起作用

ios - 一个 View 中的多个UICollectionView更新第二个UICollectionView数据

ios - 我可以在 iOS 应用程序中打开两个 EASession 来与我的外部配件通信吗?

ios - 如何将 sizeForItemAt 放在后台线程上?

swift - 改变平面中顶点的位置

ios - 核心动画立即调用完成函数

ios - 过去 24 小时 Xcode 中有 1 个版本缺少 dSYM?

ios - 来自 Firebase 数据库的 TableViewCell 数据

swift - 是否可以在 Swift iOS 应用程序中设置所有字体的字体大小?

iphone - 如何在某一点旋转 CALayer