MaxKrissigo MaxKrissigo - 1 year ago 139
Swift Question

Cross-Disolve transition just changes instantly - Swift 3.0

I'm trying to change the color of a button's background using the cross dissolve animation, however, whenever I run this it instantly change the color instead of having a smooth transition.

UIView.transition(with: self.SignIn,
options: UIViewAnimationOptions.transitionCrossDissolve,
animations: { self.SignIn.backgroundColor? = UIColor(hexaString: "#" + hex) },
completion: nil)

Answer Source

Instead of setting backgroundColor in animations block I have set it before the view transition and then start the transition of view and it will work perfectly.

self.btnAnimate.backgroundColor =
UIView.transition(with: self.btnAnimate,
                  duration: 3,
                  options: .transitionCrossDissolve,
                  animations: nil,
                  completion: nil)


enter image description here

Note: I have set red color as backgroundColor you can set what ever color you want.