Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView gesture recognizer eating all touch events

I have a UIScrollView to which I added a single tap gesture recognizer to show/hide some UI overlay using:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:singleTap];

and:

- (void)handleTap:(UITapGestureRecognizer *)sender {
    // report click to UI changer
}

I added an easy table view to the bottom of the UIScrollView. Everything works right (scrolling both horizontally and vertically) but the problem is that taps are recognized only by the gesture recognizer (above), but not by the easy table view. If I remove The line that registers the gesture listener, everything works fine, the table view notices taps on itself.

It's as if the gesture recognizer function "eats" the tap events on the table view and doesn't propagate them downward.

Any help is appreciated

like image 520
Itai Hanski Avatar asked Jun 02 '13 12:06

Itai Hanski


3 Answers

This should solve your problem.
Detect touch event on UIScrollView AND on UIView's components [which is placed inside UIScrollView]
The idea is to tell the gesture recognizer to not swallow up the touch events. To do this you need to set singleTap's cancelsTouchesInView property to NO, which is YES by default.

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
singleTap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTap]; 
like image 183
zambrey Avatar answered Oct 21 '22 00:10

zambrey


Swift 3.0

 let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
 singleTap.cancelsTouchesInView = false
 singleTap.numberOfTapsRequired = 1
 scrollView.addGestureRecognizer(singleTap)

And the selector method be like.

@objc func handleTap(_ recognizer: UITapGestureRecognizer) {
  // Perform operation
}
like image 15
Jaydeep Vora Avatar answered Oct 21 '22 00:10

Jaydeep Vora


I think the reason is that User Interaction Enabled is set to false for UIImageView. You should set it to true to enable tapping in it

like image 9
RandyTek Avatar answered Oct 21 '22 00:10

RandyTek