Hide view and apply hide effect - Swift

Viewed 1368

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
  }
3 Answers

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.

You need to use it like this .. You need to hide button on completion of Animation instead of in Animation block because isHidden property is not animate-able so it hide button immediately

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