Swift UIView.animate不设置动画

Swift UIView.animate不设置动画,swift,Swift,我正在尝试使用UIView.animate设置UILabel textColor更改的动画。但是,当我尝试使用UIView时,没有任何变化。这是我的密码: titleLabel.textColor = UIColor(red: 104/250, green: 155/250, blue: 121/250, alpha: 1) UIView.animate(withDuration: 1.0, animations: { self.titleLabel.textColor = UIColo

我正在尝试使用UIView.animate设置UILabel textColor更改的动画。但是,当我尝试使用UIView时,没有任何变化。这是我的密码:

titleLabel.textColor = UIColor(red: 104/250, green: 155/250, blue: 121/250, alpha: 1)
UIView.animate(withDuration: 1.0, animations: {
    self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
我希望标签的颜色变为绿色,然后再变回以前的灰色。提前感谢您的帮助

试试看

 UIView.animate(withDuration: 1.0, animations: {
        self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
        self.view.layoutIfNeeded()
    }

TextColor在文档中不应设置动画,因此您必须执行以下动画代码

原因

textColor不可设置动画的原因是UILabel使用常规CALayer而不是CATextLayer。所以你有两个选择

  • 在CATextLayer上执行动画
  • 添加自定义动画层,如下代码所示

  • 在Apple developer文档中,
    textColor
    属性未指定为可设置动画,因此我认为您无法使用简单的
    UIView animations
    块来实现

    您可以改用
    ui视图转换

    代码:

    UIView.transition(with: self.titleLabel, duration: 1.0, options: .transitionCrossDissolve, animations: {
                self.titleLabel.textColor = UIColor(red: 104/250, green: 155/250, blue: 121/250, alpha: 1)
            }, completion: { _ in
                self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
            })
    

    我希望它能帮助你。如果您还有任何问题,请告诉我

    你能在屏幕上记录下现在正在显示的内容吗?动画不起作用吗?@AmirKhan是的,动画不起作用。没什么特别的,明白你的意思了。您需要一个简单的颜色变换。检查下面我的答案。
    UIView.transition(with: self.titleLabel, duration: 1.0, options: .transitionCrossDissolve, animations: {
                self.titleLabel.textColor = UIColor(red: 104/250, green: 155/250, blue: 121/250, alpha: 1)
            }, completion: { _ in
                self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
            })