【问题标题】:Change label color with animateWithDuration in Swift在 Swift 中使用 animateWithDuration 更改标签颜色
【发布时间】:2015-06-01 16:07:51
【问题描述】:

我正在尝试为标签文本设置动画,以便如果值较大,它将文本颜色更改为蓝色,如果值较小,它将颜色更改为红色,否则保持相同的“黑色”。

但是UIView.animateWithDuration() 它将颜色永久更改为蓝色,我要做的只是如果值大于或小于,我想将标签颜色更改为蓝色或红色几秒钟,然后将其颜色恢复为黑色.

这是我的代码:

@IBOutlet weak var label: UILabel!
let x = 10
let y = 20

if x > y 
{
UIView.animateWithDuration(2,animations:
            { () -> Void in self.label.textColor = UIColor.blueColor(); })
}

else if y < x
{
UIView.animateWithDuration(2,animations:
                    { () -> Void in self.label.textColor = UIColor.redColor(); })
}
else
{
 self.label.textColor = UIColor.blackColor()
}

我也尝试使用Sleep函数如下,但没有成功

self.label.textColor = UIColor.blueColor()
sleep(3)
self.label.textColor = UIColor.blackColor()

【问题讨论】:

    标签: ios swift uilabel


    【解决方案1】:

    UIView 动画 api 无法为 UILabel 的 textColor 属性设置动画,为此您需要使用 CAAnimation。这是一个使用 CATransition 的实现。

     func animate() {
        let x = 10
        let y = 20
    
        var finalColor: UIColor!
    
        if x > y {
            finalColor = UIColor.blueColor()
        } else {
            finalColor = UIColor.redColor()
        }
    
        let changeColor = CATransition()
        changeColor.type = kCATransitionFade
        changeColor.duration = 2.0
    
        CATransaction.begin()
    
        CATransaction.setCompletionBlock {
            self.label.textColor = UIColor.blackColor()
            self.label.layer.addAnimation(changeColor, forKey: nil)
        }
        self.label.textColor = finalColor
        self.label.layer.addAnimation(changeColor, forKey: nil)
    
        CATransaction.commit()
    }
    

    【讨论】:

    • 谢谢,这就是我想要的。 :)
    【解决方案2】:

    基于 Acluda 的回答,我建议将他的代码放在 animateWithDuration:animations:completion 变体的完成处理程序中。

    UIView.animateWithDuration(2,
        animations: { () -> Void in self.label.textColor = UIColor.blueColor(); },
        completion: { (wentThrough: Bool) -> Void in
            { UIView.animateWithDuration(2,
                  animations: { () -> Void in self.label.textColor = UIColor.blackColor(); }) })
    

    【讨论】:

    • 您好感谢您的回答,但是,它将颜色标签永久更改为蓝色,处理程序没有做任何事情。我还能做些什么来做这样的事情吗?
    【解决方案3】:
    UIView.transitionWithView(myLabel, duration: 0.25, options: .TransitionCrossDissolve, animations: {() -> Void in
                label.textColor = UIColor.redColor()
            }, completion: {(finished: Bool) -> Void in
            })
    

    试试 :)

    【讨论】:

    • 我喜欢它!到目前为止,这是我最喜欢的答案。
    • @Trev14 谢谢 :)
    【解决方案4】:

    没有逻辑告诉 UIView 在第一个动画完成后返回 UIColor.blackColor。

    考虑在动画调用蓝色/红色之后添加它。

    UIView.animateWithDuration(2,animations:
            { () -> Void in self.label.textColor = UIColor.blackColor(); })
    

    【讨论】:

    • 您的代码无法运行。它和那个一样。 label.textColor = UIColor.blueColor() label.textColor = UIColor.textColor() 所以实际上它正在将颜色从蓝色变为黑色,但用户不会注意到任何东西。
    猜你喜欢
    • 2014-12-23
    • 2015-03-31
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多