【问题标题】:UIView.animate does not work on background color [duplicate]UIView.animate 不适用于背景颜色 [重复]
【发布时间】:2016-12-16 07:37:55
【问题描述】:

我有多个带有浅灰色背景颜色的标签,我想将它们的颜色设置为 UIColor.clear:

UIView.animate(withDuration: 5.0, animations: {
                    self.titleLabel.backgroundColor = UIColor.clear
                    self.dateLabel.backgroundColor = UIColor.clear
                    self.categoryLabel.backgroundColor = UIColor.clear
                })

但是颜色会立即改变!为什么?

【问题讨论】:

    标签: ios swift uiview


    【解决方案1】:

    好吧,我不知道为什么Apple不允许动画背景颜色变化背后的原因,但这里是另一个stackoverflow's post提供的解决方案,如下:

    #import <QuartzCore/QuartzCore.h>
    
    ...
    
    theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
    
    [UIView animateWithDuration:2.0 animations:^{
        theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
    } completion:NULL];
    

    另一个 hack 是你在UILabel 后面创建一个UIView,并赋予UILabel 清晰的颜色,这样你就可以动画UIView 的颜色变化

    【讨论】:

    • 是Objective-c吗? :)
    • 是的,你在 Swift 中需要它吗?
    • 不是我,@RankoR 有,请重新检查问题,代码 sn-p 是 Swift 代码,没有objective-c 标签。这就是为什么我认为它应该是 Swift。谢谢你的回答:)
    • 仅供参考:)
    • 谢谢,把它翻译成 Swift 就可以了!
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2021-11-10
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多