3
votes

I setup a UITapGestureRecognizer on a UIScrollView in the storyboard. The Scroll View contains other contents (two UIView, one UIWebView).

The Gesture Recognizer properties are as follows:

  • Action: dismissPopover
  • delegate: postViewController
  • gestureRecognizers: Scroll View
  • state: enabled
  • numberOfTapsRequired: 1
  • numberOfTouchesRequired: 1
  • cancelTouchesInView: YES
  • delayTouchesBegan: NO
  • delayTouchesEnded: YES

The Scroll View (relevant) properties are as follows:

  • userInteractionEnabled: YES
  • canCancelContentTouches: YES

However, when I tap anywhere on the Scroll View, the gesture does not work.

2
I'm not sure you can add a gesture recognizer to a scrollview as it's native gesture handling probably takes precedence. the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: from the gesturerecognizer delegate might be interesting to try to implement and see if its calledmanecosta
You can absolutely add gesture recognizers to a scrollview, but I think your scrollview's pan gesture's swallowing up your tap in this case. You have to require the scrollview's pan gesture to fail when the tap gesture's recognized.Lyndsey Scott
I will try that @LyndseyScott, thank you!entropid
Yeah definitely go with the solution @Rakesh linked to if you want to allow for both gestures to happen at once. Otherwise, you may want to require the scroll to fail during a tap.Lyndsey Scott

2 Answers

4
votes

The delegate class (conforming to UIGestureRecognizerDelegate) must implement

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

This way, the tap gesture will work.

1
votes

The scroll view has it's own gesture recognizer.

You'll need to override that gesture recognizer or disable that first.

Then and then only, your gesture recognizer will work.

The better way to do this is to use the tap gesture inside the scroll view rather than adding your gesture recognizer.