bigmountainstudio / DesignableX

This repository holds @IBDesignable files that can be used to extend the Interface Builder and make it easier to customize controls on the Storyboard.
194 stars 77 forks source link

UIButtonX tapping in quick succession #1

Closed ppearson111 closed 7 years ago

ppearson111 commented 7 years ago

Love your designable UI classes! Wondering if you've noticed what I'm seeing... when creating a button and setting it's class to UIButtonX, the resulting button can't respond to button taps that are done in quick succession; seems like there is a period of animation or something going on where the button doesn't respond to all of the taps. If I slow down the tapping to about 1 or 2 taps per second, then it seems to respond ok.

bigmountainstudio commented 7 years ago

Oh you know what, I think I know exactly what this is.

By default when something is animating, iOS makes whatever is being animated non-interactable (userInteractionEnabled = false). So let's look at the flash animation for the button tapping:

// Begin Tracking is like a touch down inside event for the button. 
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        alphaBefore = alpha

        UIView.animate(withDuration: 0.2, animations: {
            self.alpha = 0.4
        })

        return true
    }

// End Tracking is like the touch up inside event
    override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
        UIView.animate(withDuration: 0.35, animations: {
            self.alpha = self.alphaBefore
        })
    }

So while this animation is taking place, the button does not allow interaction.

There's a way to fix this. There is another UIView.animate function that takes in options as a parameter. One option is .allowUserInteraction. I'm 99% positive this will fix the issue!

If you want, you can fix it and create a PR or I can fix it after work. It basically would involve replacing the existing UIView.animate line with:

UIView.animate(withDuration: 0.2, delay: 0, options: .allowUserInteraction, animations: {

Hope this helps! Mark

ppearson111 commented 7 years ago

That did the trick - thx! Pull request submitted.