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

Mic*_*are 73 iphone core-animation ios

这看起来应该可以,但不是.颜色立刻变为绿色.

self.labelCorrection.backgroundColor = [UIColor whiteColor];
[UIView animateWithDuration:2.0 animations:^{
    self.labelCorrection.backgroundColor = [UIColor greenColor];
}];
Run Code Online (Sandbox Code Playgroud)

bos*_*acs 141

我找不到它的任何地方记录,但它出现的backgroundColor财产UILabel不能设置动画,因为你的代码工作正常与香草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];
Run Code Online (Sandbox Code Playgroud)

  • 同意.我现在只能假设Apple选择使这些属性不为"UILabel"制作动画.FWIW,`UIButton`的行为与标签相同. (3认同)
  • 奇怪的是,如果`UILabel`来自xib,至少`frame`属性似乎也不具有动画效果.如果以编程方式创建它,它确实有效,但是......很奇怪. (2认同)

Sur*_*gch 19

迅速

  1. (重要)将UILabel的背景颜色设置为清除(在IB或代码中).例如:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        myLabel.backgroundColor = UIColor.clear
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 为图层背景颜色设置动画.

    @IBAction func animateButtonTapped(sender: UIButton) {
    
        UIView.animate(withDuration: 1.0, animations: {
            self.myLabel.layer.backgroundColor = UIColor.red.cgColor
        })
    }
    
    Run Code Online (Sandbox Code Playgroud)

注意CGColor在之后添加UIColor.

结果

在此输入图像描述


sof*_*ved 7

你可以动画它,但我必须首先(编程)将它设置为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];
}
Run Code Online (Sandbox Code Playgroud)

这是我的动画例程:

-(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"];
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*rne 7

斯威夫特3

要将标签背景颜色从白色设置为绿色,请按以下方式设置标签:

self.labelCorrection.backgroundColor = UIColor.clear
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
Run Code Online (Sandbox Code Playgroud)

像这样的动画:

UIView.animate(withDuration: 2.0) { 
    self.labelCorrection.layer.backgroundColor = UIColor.green.cgColor
}
Run Code Online (Sandbox Code Playgroud)

要返回原始状态,以便再次设置动画,请确保删除动画:

self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
self.labelCorrection.layer.removeAllAnimations()
Run Code Online (Sandbox Code Playgroud)