UIButton can’t be touched while animated with UIView animateWithDuration

Swift 5

In my case, when I set button.alpha = 0, the button interaction stops working, no matter if I setup UIViewAnimationOptionAllowUserInteraction as an option.

Reason

Whenever you define the animation or not, the view’s property is applying to view’s layer immediately. Because of this, when you set the view.alpha=0, you hide the view completely.

Solution

Easy, just reduce alpha=0.1 (or even 0.05)

UIView.animate(withDuration: 2,
                       delay: 0,
                       options: [.allowUserInteraction, .overrideInheritedOptions, .curveEaseOut, .repeat, .autoreverse],
                       animations: {
                           self.button.layer.opacity = 0.01 // 0.0 will make the button unavailable to touch
        })

Leave a Comment