Benj
Benj

Reputation: 794

UIView.animation not animating colour change

My animation is not actually smoothing the colour change. Here is my code:

UIView.animate(
    withDuration: 3.0,
    animations: { () -> Void in
        myLabel!.backgroundColor! = UIColor(
            red: 1.0,
            green: 1.0,
            blue: 1.0,
            alpha: 1.0
        )
    },
    completion: nil
)

This code is wrapped in an if statement, which in turn is inside a function. myLabel is the only parameter; and it's of type UILabel?

My app has a UILabel with a pan gesture. When the user swipes up, the colour should change. If the background colour (which is another label) is black, myLabel should animate becoming white. (This is the if statement)

I've had this same problem multiple times in my app and it's the same code each time. There's no error, but no animation.

Upvotes: 1

Views: 152

Answers (1)

Aman Pathak
Aman Pathak

Reputation: 219

You can try this code

 UIView.transition(with: myLabel, duration: 0.4, options: .transitionCrossDissolve, animations: {
        self.myLabel.backgroundColor! = UIColor.init(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    }) { (finish) in
    }

Upvotes: 3

Related Questions