尝试对一个UILabel的backgroundColor进行动画处理以实现“淡出”效果,但效果瞬间应用

4

我想要通过动画来实现uilabel的背景颜色渐变效果。我的代码如下:

UILabel *lbl = ...
UIColor *oldCol = lbl.backgroundColor;
lbl.backgroundColor = [UIColor blueColor];
[UIView animateWithDuration:1.0 animations:^(void) {
    lbl.backgroundColor = oldCol;
}];

但它立即恢复到原来的颜色。我还尝试了以下方法,以隔离问题:
lbl.backgroundColor = [UIColor blueColor];
[UIView animateWithDuration:1.0 animations:^(void) {
    lbl.backgroundColor = [UIColor greenColor];
}];

并且它立即变为绿色。有任何想法吗?
2个回答

4

0

UILabel 的背景颜色本身不可动画化,但是 UILabel的背景颜色是可以动画化的。因此不要设置主要的背景颜色,否则它会隐藏层的颜色。只需将主要背景设置为透明,这样层就可以显示出来。

myLabel.backgroundColor = UIColor.clearColor()

如果你需要设置初始颜色,可以这样做:
myLabel.layer.backgroundColor = UIColor.blueColor().CGColor

然后要将这个颜色动画到新的颜色,您可以执行以下操作:

UIView.animateWithDuration(1.0, animations: {

    self.myLabel.layer.backgroundColor = UIColor.redColor().CGColor
})

这个答案是用Swift写的,但是思路是一样的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接