swiftswift3uiviewanimationuiviewanimationtransition

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,
                  duration:3,
                  options: UIViewAnimationOptions.transitionCrossDissolve,
                  animations: { self.SignIn.backgroundColor? = UIColor(hexaString: "#" + hex) },
                  completion: nil)

Solution

  • 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 = UIColor.red
    UIView.transition(with: self.btnAnimate,
                      duration: 3,
                      options: .transitionCrossDissolve,
                      animations: nil,
                      completion: nil)
    

    Output

    enter image description here

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