Reputation: 40641
I'm trying to animate backgroundcolor on a uilabel to achieve a 'fade' effect. My code looks like this:
UILabel *lbl = ...
UIColor *oldCol = lbl.backgroundColor;
lbl.backgroundColor = [UIColor blueColor];
[UIView animateWithDuration:1.0 animations:^(void) {
lbl.backgroundColor = oldCol;
}];
But it instantly reverts to the original colour. I also tried the below, to isolate the problem:
lbl.backgroundColor = [UIColor blueColor];
[UIView animateWithDuration:1.0 animations:^(void) {
lbl.backgroundColor = [UIColor greenColor];
}];
And it instantly goes to green. Any ideas?
Upvotes: 4
Views: 1545
Reputation: 512296
The UILabel
background color itself isn't animatable, but the UILabel
layer background color is. So don't set the main background color, otherwise it will hide the layer color. Just make the main background clear so that the layer shows through.
myLabel.backgroundColor = UIColor.clearColor()
If you need to set an initial color you can do it like this:
myLabel.layer.backgroundColor = UIColor.blueColor().CGColor
Then to animate this color to a new one you can do the following:
UIView.animateWithDuration(1.0, animations: {
self.myLabel.layer.backgroundColor = UIColor.redColor().CGColor
})
This answer is in Swift, but the idea is the same.
Upvotes: 0
Reputation: 8237
Background color of UILabel isn't animatable. See How to animate the background color of a UILabel? for a possible workaround.
Upvotes: 4