ScrollView gesture recognizer eating all touch events

僤鯓⒐⒋嵵緔 提交于 2019-11-26 21:59:15
zambrey

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]; 
Jaydeep

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
}
sangony

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;
}

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

Nuno Vieira

Thanks @zambrey

Swift 2.2+ Version:

scrollView.delegate = self

let allowMultipleTouches = UITapGestureRecognizer(target: self, action: #selector(genderPressed))
allowMultipleTouches.numberOfTapsRequired = 1
allowMultipleTouches.cancelsTouchesInView = false

scrollView.addGestureRecognizer(allowMultipleTouches)

If your scroll view is in the Storyboard, don't forget to pin the Outlet in the view controller. In this example, scrollView is the Outlet of the UIScrollView.

hamishkeith

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!

TapGestures worked for me. The swipe on the other hand, I had to disable the scrolling and it worked.

     swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
     swipeLeftGesture.direction = .left
     scrollView.addGestureRecognizer(swipeLeftGesture)

     swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
     scrollView.addGestureRecognizer(swipeRightGesture)

    scrollView.isScrollEnabled = false

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(_:)))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!