UIView.animate不适用于背景颜色

时间:2016-12-16 07:37:56

标签: ios swift uiview

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

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

但颜色瞬间变化!为什么呢?

1 个答案:

答案 0 :(得分:1)

好吧,我不知道为什么苹果不允许动画背景颜色变化的原因,但这是另一个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];

另一个黑客是你在UIView后面UILabel,并给UILabel清晰的颜色,这样你就可以设置UIView的颜色变化动画