ScrollView gesture recognizer eating all touch eve

2019-01-06 13:35发布

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

8条回答
Juvenile、少年°
2楼-- · 2019-01-06 13:45

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

查看更多
姐就是有狂的资本
3楼-- · 2019-01-06 13:46

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]; 
查看更多
\"骚年 ilove
4楼-- · 2019-01-06 13:46

This worked for me in Swift 3 / Xcode 8

    self.scrollView.touchesShouldCancel(in: ** the view you want the touches in **)
    self.scrollView.canCancelContentTouches = false

Good luck!

查看更多
女痞
5楼-- · 2019-01-06 13:47

You can capture any kind of gestures in the UIscrollView. Make sure you also handle some of the default properties as well like set cancelsTouchesInView property to false, it is true by default. Also give some tag nos to your sub views to distinguish in selectors. & also enable their User interaction to true.

let tap = UITapGestureRecognizer(target: self, action:

selector(didTapByUser(_:)))

查看更多
欢心
6楼-- · 2019-01-06 13:52

You can set which objects are to be included/excluded for touches.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {            
   if (touch.view == [self view]) {
       return YES;
   }
   return NO;
}
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-06 13:57

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
}
查看更多
登录 后发表回答