Search code examples
iosswiftvisibilityrx-swiftrx-cocoa

Hide view and apply hide effect - Swift


I want to give an alpha effect when closing the button's visibility with the code below. However, in the code below, the alpha effect works correctly, but instantly becomes , without waiting for 0.5 seconds of visibility.

Do you have alternative suggestions to solve this? Especially if you have a solution with RxSwift, RxCoca, it would be nice. Thanks.

self.button.alpha = 1.0
UIView.animate(withDuration: 0.5) {
          self.button.alpha = 0
          self.button.isHidden = true
  }

Solution

  • Use it like this

            UIView.animate(withDuration: 0.5, animations: {
                self.button.alpha = 0
            }) { (_) in
                self.button.isHidden = true
            }
    

    Hide the button after your view's alpha has changed to 0. The issue in your code is that the button gets hidden in the animation block so the animation happens when the view is already hidden.