如何为UILabel的背景色设置动画?

看起来应该可以,但不能。颜色立即变为绿色。


self.labelCorrection.backgroundColor = [UIColor whiteColor];

[UIView animateWithDuration:2.0 animations:^{

    self.labelCorrection.backgroundColor = [UIColor greenColor];

}];


守着星空守着你
浏览 483回答 3
3回答

翻翻过去那场雪

我在任何地方都找不到文档,但是它的backgroundColor属性似乎UILabel不是可动画的,因为您的代码可以在vanilla上正常工作UIView。但是,只要您不设置标签视图本身的背景颜色,此hack似乎就可以起作用:#import <QuartzCore/QuartzCore.h>...theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;[UIView animateWithDuration:2.0 animations:^{&nbsp; &nbsp; theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;} completion:NULL];

慕容708150

您可以为其设置动画,但是由于某种原因,我必须首先(以编程方式)将其设置为clearColor。否则,动画要么不起作用,要么不可见。我正在自定义表格单元中设置UILabel的背景色。这是willDisplayCell方法中的代码。我不会尝试在cellForRow中设置动画,因为很多布局都被表格修改了。- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{&nbsp; &nbsp; &nbsp; &nbsp; ((RouteListCell *)cell).name.backgroundColor = [UIColor clearColor];&nbsp; &nbsp; &nbsp; &nbsp; [((RouteListCell *)cell).name backgroundGlowAnimationFromColor:[UIColor whiteColor] toColor:[UIColor redColor] clearAnimationsFirst:YES];}这是我的动画例程:-(void) backgroundGlowAnimationFromColor:(UIColor *)startColor toColor:(UIColor *)destColor clearAnimationsFirst:(BOOL)reset;{&nbsp; &nbsp; if (reset)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [self.layer removeAllAnimations];&nbsp; &nbsp; }&nbsp; &nbsp; CABasicAnimation *anAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];&nbsp; &nbsp; anAnimation.duration = 1.00;&nbsp; &nbsp; anAnimation.repeatCount = HUGE_VAL;&nbsp; &nbsp; anAnimation.autoreverses = YES;&nbsp; &nbsp; anAnimation.fromValue = (id) startColor.CGColor; // [NSNumber numberWithFloat:1.0];&nbsp; &nbsp; anAnimation.toValue = (id) destColor.CGColor; //[NSNumber numberWithFloat:0.10];&nbsp; &nbsp; [self.layer addAnimation:anAnimation forKey:@"backgroundColor"];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS