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条回答
【Aperson】
2楼-- · 2019-01-06 14:02

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
查看更多
姐就是有狂的资本
3楼-- · 2019-01-06 14:05

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.

查看更多
登录 后发表回答