4

EDIT: I would actually like to animate the textColor property. I thought it would be the same solution as for backgroundColor but apparently not because textColor is not available in layer. I found the solution to this here.

I'm trying to change the color of the background of a UILabel randomly every one second. The code I am using works fine on a regular view, but is iterating way too fast when I call it on the label. Its as though animate with duration is not taking any time on the label. Any idea why this is? I actually would like to change the label text color so that's why its important it works on the label.

colorChanges(v: lbl) // TOO FAST!
colorChanges(v: someView) // PERFECT
colorChanges(v: self.view) // PERFECT

Animation Code

func colorChanges(v: UIView) {
    UIView.animate(withDuration: 1.0, animations: {
        v.backgroundColor = UIColor(displayP3Red: CGFloat(arc4random()) / CGFloat(UInt32.max), green: CGFloat(arc4random()) / CGFloat(UInt32.max), blue: CGFloat(arc4random()) / CGFloat(UInt32.max), alpha: 1.0)
    }, completion: {
        (value: Bool) in
        self.colorChanges(v: v)
    })
}

Weird Behavior

enter image description here

Anters Bear
  • 1,816
  • 1
  • 15
  • 41
  • https://stackoverflow.com/questions/3762561/how-to-animate-the-background-color-of-a-uilabel – Joe May 27 '18 at 08:42
  • 2
    Possible duplicate of [How to animate the background color of a UILabel?](https://stackoverflow.com/questions/3762561/how-to-animate-the-background-color-of-a-uilabel) – Guy Kogus May 27 '18 at 08:43
  • for some reason the `UILabel` animation complete instantly, you can trace the problem in this direction –  May 27 '18 at 08:44
  • Hi thank you for your replies! The other posts you guys linked provide a solution for changing the background color. Any idea how I might do this for the textColor? – Anters Bear May 27 '18 at 09:06

1 Answers1

1

try below changed method:

func colorChanges(v: UIView) {
    UIView.animate(withDuration: 1.0, animations: {
            v.layer.backgroundColor = UIColor(displayP3Red: CGFloat(arc4random()) / CGFloat(UInt32.max), green: CGFloat(arc4random()) / CGFloat(UInt32.max), blue: CGFloat(arc4random()) / CGFloat(UInt32.max), alpha: 1.0).cgColor
        }, completion: { [weak self]
            (value: Bool) in
            self?.colorChanges(v: v)
        })
    }

Please, try updated answer for label textColor animation:

        UIView.transition(with: v, duration: 1.0, options:.transitionCrossDissolve, animations: {
            v.backgroundColor = UIColor(displayP3Red: CGFloat(arc4random()) / CGFloat(UInt32.max), green: CGFloat(arc4random()) / CGFloat(UInt32.max), blue: CGFloat(arc4random()) / CGFloat(UInt32.max), alpha: 1.0)

            if let label = v as? UILabel {
                label.textColor = UIColor(displayP3Red: CGFloat(arc4random()) / CGFloat(UInt32.max), green: CGFloat(arc4random()) / CGFloat(UInt32.max), blue: CGFloat(arc4random()) / CGFloat(UInt32.max), alpha: 1.0)
            }
        }) { [weak self] (success) in
            self?.colorChanges(v: v)
        }
Moayad Al kouz
  • 1,342
  • 1
  • 9
  • 19