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

时间:2010-09-21 16:51:15

标签: ios iphone core-animation

这看起来应该有效,但事实并非如此。颜色立刻变为绿色。

self.labelCorrection.backgroundColor = [UIColor whiteColor];
[UIView animateWithDuration:2.0 animations:^{
    self.labelCorrection.backgroundColor = [UIColor greenColor];
}];

11 个答案:

答案 0 :(得分:140)

我无法在任何地方找到它,但看起来backgroundColor的{​​{1}}属性不可动画,因为您的代码可以使用香草UILabel正常工作。但是,只要您没有设置标签视图本身的背景颜色,该黑客似乎就可以工作:

UIView

答案 1 :(得分:18)

夫特

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

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

    @IBAction func animateButtonTapped(sender: UIButton) {
    
        UIView.animate(withDuration: 1.0, animations: {
            self.myLabel.layer.backgroundColor = UIColor.red.cgColor
        })
    }
    
  3. 请注意,CGColor之后会添加UIColor

    <强>结果

    enter image description here

答案 2 :(得分: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];
}

这是我的动画例程:

-(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"];
}

答案 3 :(得分:6)

Swift 3

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

UIView.animate(withDuration: 2.0) { 
    self.labelCorrection.layer.backgroundColor = UIColor.green.cgColor
}

像这样的动画:

self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
self.labelCorrection.layer.removeAllAnimations()

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

{{1}}

答案 4 :(得分:2)

以某种合理的帧速率(比如20 fps),根据NSTimer或CADisplayLink回调手动执行颜色动画。您必须根据整个动画时间内的分数经过时间(在您的示例中为2.0秒)计算您自己的RGB或HSV颜色变化曲线,或使用40种中间颜色的数组。

答案 5 :(得分:2)

请试试这个,

[UIView transitionWithView:yourView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{

[yourView setBackgroundColor:[UIColor colorWithRed:0.8588 green:0.8588 blue:0.8588 alpha:1]];
    }completion:^(BOOL finished) {

 }];

它对我有用。

答案 6 :(得分:2)

我最近再次遇到这个问题,经过一些研究后,我发现基本上没有任何变化(可能在可预见的未来也没有变化),所以我最终使用了Facebook POP framework(不仅仅是)。

您可以轻松地在视图和图层上执行基本属性的动画,但此外,它还提供了颜色之间的平滑过渡(基本上无论您想要什么,甚至是自定义类型,还有一些额外的编码)。所以对于背景颜色,你会做这样的事情:

   // Create spring animation (there are more types, if you want)
   let animation = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)

   // Configure it properly
   animation.autoreverses = false
   animation.removedOnCompletion = true
   animation.fromValue = UIColor.yourBeginningColor()
   animation.toValue = UIColor.yourFinalColor()

   // Add new animation to your view - animation key can be whatever you like, serves as reference
   label.pop_addAnimation(animation, forKey: "YourAnimationKey")

Viola,现在你可以在具有该属性的所有内容上设置背景颜色的动画!

答案 7 :(得分:1)

答案 8 :(得分:1)

如果你不想深入到Quartz,每回答一个前面的答案,创建一个与UILabel大小相同的空UIView,并将它放在与UILabel相同的位置(并按Z顺序,在它下面)并且你可以为UIView的背景颜色设置动画(我试过这个,它对我有用)。

答案 9 :(得分:0)

以下是参考资料:

  

动画包含的块对象   提交视图的更改。   这是您以编程方式进行的   改变任何可动画的属性   视图层次结构中的视图。这个   块没有参数,没有参数   回报价值。此参数不得   是NULL

我对此的理解是,当您的视图调用动画块时,从那时起您的视图标签背景颜色将被设置为绿色。然后,如果您不将标签背景颜色更改为其他颜色,则它将始终为绿色。这就是我猜的

答案 10 :(得分:0)

Swift 4.1 UILabel扩展名:

将此扩展名添加到您的代码中:

extension UILabel {

    /** Animates the background color of a UILabel to a new color. */
    func animateBackgroundColor(to color : UIColor?, withDuration : TimeInterval, completion : ((Bool) -> Void)? = nil) {
        guard let newColor = color else { return }
        self.backgroundColor = .clear
        UIView.animate(withDuration: withDuration, animations: {
            self.layer.backgroundColor = newColor.cgColor
        }, completion: completion)
    }

}

您应该这样使用它:

myUILabel.animateBackgroundColor(to: .red, withDuration: 0.5)

现在,如果要恢复原始颜色,请按以下方式使用它:

// Storing the orignal color:
let originalColor = myUILabel.backgroundColor

// Changing the background color:
myUILabel.animateBackgroundColor(to: .red, withDuration: 0.5)

// ... Later:

// Restoring the original color:
myUILabel.animateBackgroundColor(to: originalColor, withDuration: 0.5, completion: {
    (value: Bool) in
    myUILabel.backgroundColor = originalColor
})
相关问题