为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
守着星空守着你 2019-12-13 17:10:29
看起来应该可以,但不能。颜色立即变为绿色。self.labelCorrection.backgroundColor = [UIColor whiteColor];[UIView animateWithDuration:2.0 animations:^{    self.labelCorrection.backgroundColor = [UIColor greenColor];}];
查看完整描述

3 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

我在任何地方都找不到文档,但是它的backgroundColor属性似乎UILabel不是可动画的,因为您的代码可以在vanilla上正常工作UIView。但是,只要您不设置标签视图本身的背景颜色,此hack似乎就可以起作用:


#import <QuartzCore/QuartzCore.h>


...


theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;


[UIView animateWithDuration:2.0 animations:^{

    theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;

} completion:NULL];



查看完整回答
反对 回复 2019-12-14
?
慕容708150

TA贡献1831条经验 获得超4个赞

您可以为其设置动画,但是由于某种原因,我必须首先(以编程方式)将其设置为clearColor。否则,动画要么不起作用,要么不可见。


我正在自定义表格单元中设置UILabel的背景色。这是willDisplayCell方法中的代码。我不会尝试在cellForRow中设置动画,因为很多布局都被表格修改了。


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

        ((RouteListCell *)cell).name.backgroundColor = [UIColor clearColor];

        [((RouteListCell *)cell).name backgroundGlowAnimationFromColor:[UIColor whiteColor] toColor:[UIColor redColor] clearAnimationsFirst:YES];

}

这是我的动画例程:


-(void) backgroundGlowAnimationFromColor:(UIColor *)startColor toColor:(UIColor *)destColor clearAnimationsFirst:(BOOL)reset;

{

    if (reset)

    {

        [self.layer removeAllAnimations];

    }


    CABasicAnimation *anAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];

    anAnimation.duration = 1.00;

    anAnimation.repeatCount = HUGE_VAL;

    anAnimation.autoreverses = YES;

    anAnimation.fromValue = (id) startColor.CGColor; // [NSNumber numberWithFloat:1.0];

    anAnimation.toValue = (id) destColor.CGColor; //[NSNumber numberWithFloat:0.10];

    [self.layer addAnimation:anAnimation forKey:@"backgroundColor"];

}



查看完整回答
反对 回复 2019-12-14
  • 3 回答
  • 0 关注
  • 479 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信