I am trying to animate backgroundColor
property of UILabel class and being unsuccessful so far. Here the snippet of my code
-(void) blink {
UIColor* originalColor = lblDescription.backgro开发者_C百科undColor;
lblDescription.backgroundColor = [UIColor yellowColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
lblDescription.backgroundColor = originalColor;
[UIView commitAnimations];
}
//this code works if lblDescription is UIView and does not if UILabel
I found some claims that some UILabel properties are not animatable, but I couldn't substantiate that claim by reading Apple docs. I was wondering if someone can shed light on this problem.
On this page from the View Programming Guide For iOS, "Table 1-2 Animatable properties" supposedly lists all the animatable properties of UIViews. It doesn't include backgroundColor.
But on the UIView Class Reference, under the backgroundColor property it says:
Discussion
Changes to this property can be animated. The default is nil.
So it's not clear why the UILabel's backgroundColor does not animate.
The workaround I've used in the past is to use a CATextLayer instead of a UILabel.
If you'd like to animate the color of a label (including the background) you can do something like this:
https://stackoverflow.com/a/20892927/189924
This does not involve layers or using multiple views.
精彩评论