CoreAnimation — 在动画开始之前,层会闪烁到最终值吗?

Mat*_* H. 1 core-animation calayer cabasicanimation ios

我正在尝试对图层的背景颜色从红色到蓝色进行简单的 CABasicAnimation。

添加动画并设置模型层的最终值后,动画闪烁为蓝色,然后再次变为红色,动画变为蓝色。

我的代码是:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // Create red layer and add to view
    let redLayer = CALayer()
    redLayer.backgroundColor = UIColor.red.cgColor
    redLayer.position = view.center
    redLayer.bounds.size = CGSize(width: 100, height: 100)
    view.layer.addSublayer(redLayer)

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {

        // After a 1 second delay, animate from red to blue.

        let anim = CABasicAnimation(keyPath: "backgroundColor")
        anim.duration = 3
        anim.fromValue = redLayer.backgroundColor
        anim.toValue = UIColor.blue.cgColor
        redLayer.add(anim, forKey: "")

        // Set background color to final value
        redLayer.backgroundColor = UIColor.blue.cgColor
    }
}
Run Code Online (Sandbox Code Playgroud)

这里发生了什么事?

在此处输入图片说明

Dav*_*ist 5

简短的回答是,当您更新图层的隐式动画属性(backgroundColor在本例中)时,您将获得隐式动画。

这个 0.25 秒的动画优先于您的显式动画,因为它是在之后添加的。之所以会发生这种隐式动画,是因为该层是“独立的”(即,它不属于视图)。

要摆脱这种隐式动画,您可以CATransaction仅为更新图层属性的范围创建和禁用动作:

CATransaction.begin()
CATransaction.setDisableActions(true)

redLayer.backgroundColor = UIColor.blue.cgColor

CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)

作为禁用隐式动画的替代方法,您还可以在添加显式动画之前更新图层。如果您想了解更多信息,我对在此答案中添加动画之前或之后更新图层的影响进行了相当详细的解释。