动画CALayer背景颜色并更新模型值 [英] Animating CALayer background color and update model value

查看:315
本文介绍了动画CALayer背景颜色并更新模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为UIView中子图层的 backgroundColor 动画化(在 tintColorDidChange 上)。

I want to animate a backgroundColor change for a sublayer in my UIView (on tintColorDidChange).

我需要将图层的当前背景色动画化为新的色调颜色几次(每次都使用不同的色调颜色),所以需要更新backgroundColor的模型值(我可以不要在动画上使用 removedOnCompletion = false

I need to animate from the layer's current background colour to the new tint colour several times (different tint colours each time), so the model value for backgroundColor needs to be updated (I can't use removedOnCompletion = false on the animation).

使用CABasicAnimation我可以让变色动画正常工作,如果我不更新模型值(但当然,动画完成后颜色会重置)。
当我尝试更新模型值时,颜色变化会立即发生并且动画丢失。

Using CABasicAnimation I have the colour change animation working fine if I don't update the model value (but of course the colour resets after the animation is complete). When I try to update the model value the colour change happens immediately and the animation is lost.

我试图禁用隐式动画并更新模型值 CATransation ,但动画仍然丢失。

I attempted to disable the implicit animation and update the model value with CATransation but the animation is still lost.

如何更新 backgroundColor 模型值并保持淡入淡出动画正常工作?

How can I update the backgroundColor model value and keep my fade animation working?

override func tintColorDidChange() {
    super.tintColorDidChange()

    let colourAnim = CABasicAnimation(keyPath: "backgroundColor")
    colourAnim.toValue = self.tintColor.CGColor
    colourAnim.duration = 1.0
    self.spinnerLayer?.addAnimation(colourAnim, forKey: "colourAnimation")

    CATransaction.begin()
    CATransaction.setDisableActions(true)
    self.spinnerLayer?.backgroundColor = self.tintColor.CGColor
    CATransaction.commit()
}


推荐答案

使用显式的来回动画的mValue

override func tintColorDidChange() {
    super.tintColorDidChange()

    let colourAnim = CABasicAnimation(keyPath: "backgroundColor")
    colourAnim.fromValue = self.spinnerLayer!.backgroundColor
    colourAnim.toValue = self.tintColor.CGColor
    colourAnim.duration = 1.0
    self.spinnerLayer?.addAnimation(colourAnim, forKey: "colourAnimation")
    self.spinnerLayer?.backgroundColor = self.tintColor.CGColor

}

这篇关于动画CALayer背景颜色并更新模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆