如何animationUILabel的背景颜色?

这看起来应该工作,但不是。 颜色立即变成绿色。

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

我无法find它在任何地方logging,但它似乎UILabelbackgroundColor属性不是animation,因为你的代码工作正常与香草UIView 。 但是,只要不设置标签视图本身的背景颜色,这种黑客行为似乎就行得通:

 #import <QuartzCore/QuartzCore.h> ... theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor; [UIView animateWithDuration:2.0 animations:^{ theLabel.layer.backgroundColor = [UIColor greenColor].CGColor; } completion:NULL]; 

迅速

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

     override func viewDidLoad() { super.viewDidLoad() myLabel.backgroundColor = UIColor.clear } 
  2. animation层的背景颜色。

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

请注意,在UIColor之后添加CGColor

结果

在这里输入图像说明

你可以animation,但我必须首先设置(编程)clearColor出于某种原因。 没有这个,animation要么不工作要么不可见。

我在UILabel的自定义表格单元格的背景颜色animation。 这是willDisplayCell方法中的代码。 我不会尝试在cellForRow中设置animation,因为大量的布局被表格所玷污。

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

这是我的animation程序:

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

根据NSTimer或CADisplayLinkcallback以某种合理的帧速率(例如20 fps)手动进行彩色animation。 您将必须根据完整animation时间(在您的示例中为2.0秒)中的部分stream逝时间计算RGB或HSV自己的颜色变化曲线,或者使用40个中间色的数组。

最近我又遇到了这个问题,经过一番调查,我发现基本上没有什么变化(也许不会在可预见的将来),所以我最终使用了Facebook POP框架 (不仅仅是这个)。

你可以很容易地在视图和图层上做基本属性的animation,但是除此之外,它还提供了颜色之间的平滑过渡(基本上你想要的就是你自定义的types,还有一些额外的编码)。 所以对于背景颜色,你可以这样做:

  // 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") 

中提琴,现在你可以在任何有财产的背景颜色animation!

首先,我没有尝试过我自己,但在过去的几个星期里我已经考虑过了。 我想,你的视图的颜色属性不能animation,就像“隐藏”,变化会立即出现。 一个可能的解决scheme是将两个标签叠加在上面,旧的颜色与下面的颜色相同。 然后,你可以简单地淡出上面的一个。 我不确定,如果混合看起来很好。

如果你不想陷入Quartz中,根据前面的回答之一,创build一个与UILabel相同大小的空UIView,并将它放在与UILabel相同的位置(按Z顺序,在它下面)使UIView的背景颜色动起来(我已经试过了,而且适用于我)。

请试试这个,

 [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) { }]; 

这个对我有用。

这里是参考:

animation包含提交到视图的更改的块对象。 这是以编程方式更改视图层次结构中视图的任何animation属性的位置。 该块不带参数,没有返回值。 该参数不能为NULL

我所理解的是,当你的视图调用animation块时,那么你的视图标签背景色被承诺为绿色。 那么,如果你不改变标签的背景颜色,那么它将永远是绿色的。 这是我猜的

Swift 3

要将您的标签背景色从白色变为绿色,请设置您的标签,如下所示:

 self.labelCorrection.backgroundColor = UIColor.clear self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor 

像这样animation:

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

要回到原来的状态,所以你可以再次animation,确保你删除animation:

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