1

I have multiple labels with lightGray background color, and I want to animate their colors to UIColor.clear:

UIView.animate(withDuration: 5.0, animations: {
                    self.titleLabel.backgroundColor = UIColor.clear
                    self.dateLabel.backgroundColor = UIColor.clear
                    self.categoryLabel.backgroundColor = UIColor.clear
                })

But color changes instantly! Why?

artem
  • 16,382
  • 34
  • 113
  • 189

1 Answers1

2

Well, I dont know the reason behind this why Apple didnt allow to animate the background color change, but here is a solution provided in another stackoverflow's post, which is as follows:

#import <QuartzCore/QuartzCore.h>

...

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

[UIView animateWithDuration:2.0 animations:^{
    theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
} completion:NULL];

Another hack is that you make a UIView behind UILabel, and give UILabel clear color, this way you can animate the color change of UIView

Community
  • 1
  • 1
Munahil
  • 2,381
  • 1
  • 14
  • 24