UIButton inside UIScrollView delay when tap

853 views Asked by At

I'm new in using swift and I have a problem when create it

I'm using swift 2 XCode 7

I have a button at the bottom of scrollview to submit all textfield that I put above but when I tap the button I have to hold for a few second before the button is tapped. I have search for a solution in a few web and there is a solution said that unchecked the Delays Content Touches but my scrollview become too hard to scroll so I checked another solution said that create a custom ScrollView, but after I created it, I can't tap my button.

This is several web that I found but no one worked

UIButton touch is delayed when in UIScrollView

UIButton not showing highlight on tap in iOS7 - Swift

Subclass UIScrollView in Swift for touches Began & touches Moved

Please give me some hint to some example how to fix this problem

Any help is appreciated

2

There are 2 answers

4
Lumialxk On

The best way to solve it is to separate button and scroll view frame. And the OS will not check the touch events is for button or for scroll view.

Update:

I'm wondering your button is subview of scroll view. Because I tested this view hierarchy and didn't get your issue:

enter image description here

Update 2: You can also do like above. At the same time,you should move that button whenever scroll view did scroll. This will look like they're together.

Another solution is to change response chain. You can get hit events in - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event of UIView. And if that point is in the frame of button, you should hit test button and return that result. Like this:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.clipsToBounds && !self.hidden && self.alpha > 0.01) {
        CGPoint point = [button convertPoint:point fromView:scrollView];
        if (button frame contains point) {
            UIView *result = [button hitTest:point withEvent:event];
            if (result != nil) {
                return result;
            }
        } else {
            // hit the scroll view area outside button
        }
    }

    return nil;
}
2
Ben M. On

If the button is at the bottom of the tableview, you could always add a row with a cell that acts as a button. Or add the button to the tableview footer.