4

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
  }
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
Ümit Bülbül
  • 512
  • 2
  • 15

3 Answers3

4

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.

2

self.button.isHidden = true within the animation block is causing the immediate disappearance as this property is not animatable.
Animating the alpha is what you should keep.

Solution:

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

RxSwift solution:

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
1

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
    }
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49