代码之家  ›  专栏  ›  技术社区  ›  Michiel de Mare

如何设置uilabel背景色的动画?

  •  72
  • Michiel de Mare  · 技术社区  · 14 年前

    这看起来应该管用,但不行。颜色马上变绿了。

    self.labelCorrection.backgroundColor = [UIColor whiteColor];
    [UIView animateWithDuration:2.0 animations:^{
        self.labelCorrection.backgroundColor = [UIColor greenColor];
    }];
    
    11 回复  |  直到 6 年前
        1
  •  139
  •   bosmacs    14 年前

    我在任何地方都找不到它的文档,但它似乎 backgroundColor 性质 UILabel 不是可动画的,因为您的代码可以与普通的 UIView . 不过,只要您不设置标签视图本身的背景色,此黑客似乎可以工作:

    #import <QuartzCore/QuartzCore.h>
    
    ...
    
    theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
    
    [UIView animateWithDuration:2.0 animations:^{
        theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
    } completion:NULL];
    
        2
  •  18
  •   Suragch Shmidt    8 年前

    SWIFT

    1. ( important )将uilabel的背景色设置为clear(在ib或code中)。例如:

      override func viewdidload()。{
      super.viewdidload()。
      
      mylabel.backgroundcolor=uicolor.clear
      }
      
    2. 设置图层背景颜色的动画。

      @ibaction func animatebuttonttapped(sender:uibutton){
      
      uiview.animate(持续时间:1.0,动画:{
      self.mylabel.layer.backgroundcolor=uicolor.red.cgcolor
      })
      }
      

    请注意, cgcolor 添加在 uicolor 之后。

    结果

    override func viewDidLoad() {
        super.viewDidLoad()
    
        myLabel.backgroundColor = UIColor.clear
    }
    
  • 动画层背景色。

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

    结果

    enter image description here

        3
  •  7
  •   software evolved    11 年前

    你可以设置它的动画,但出于某种原因,我必须先(通过编程)将其设置为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"];
    }
    
        4
  •  6
  •   Mike Taverne    7 年前

    斯威夫特3

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

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

    像这样动画:

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

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

    self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
    self.labelCorrection.layer.removeAllAnimations()
    
        5
  •  2
  •   hotpaw2    14 年前

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

        6
  •  2
  •   bummi Haisum Usman    11 年前

    请试试这个,

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

    它对我有用。

        7
  •  2
  •   Jiri Trecak    9 年前

    我最近又遇到了这个问题,经过一些研究,我发现基本上没有什么变化(在可预见的将来可能不会改变),所以我最终使用了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")
    

    维奥拉,现在你可以在所有有这个属性的东西上设置背景色的动画了!

        8
  •  1
  •   hfossli    13 年前
        9
  •  1
  •   David Neiss    13 年前

    如果你不想深入到石英中,根据前面的答案之一,创建一个与uilabel大小相同的空uiview,并将其放置在与uilabel相同的位置(按z顺序,在它下面),你可以动画显示uiview的背景色(我试过,它对我有效)。

        10
  •  0
  •   vodkhang    14 年前

    参考文献如下:

    动画块对象包含 要提交到视图的更改。 这是你编程的地方 更改的任何可设置动画的属性 视图层次结构中的视图。这个 块不接受参数并且没有 返回值。此参数不能 为零

    我理解的是,当你的视图调用动画块时,你的视图标签背景色从那时起就被指定为绿色。然后,如果不将标签背景色更改为其他颜色,则标签背景色将始终为绿色。这就是我想的

        11
  •  0
  •   ThiagoAM    6 年前

    Swift 4.1 ui标签扩展:

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

    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
    })