Intercepting pan gestures over a UIScrollView breaks scrolling

耗尽温柔 提交于 2019-11-28 18:10:34

OK, I figured it out. I needed to do 2 things to make this work:

1) Attach my own pan recognizer to the scroll view itself, not to another view on top of it.

2) This UIGestureRecognizerDelegate method prevents the goofy behavior that happens when both the default scrollview and my own one are invoked simultaneously.

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

I had the same problem to solve and I did this:

1) Attach my own pan recognizer to the scroll view.

2) Return YES on: – gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

This will allow both gestures to work. So what that means is that on vertical scroll, both your panGesture delegate and scrollView Delegate will be fired. If it is a horizontal scroll, it will only call your panGesture delegate.

3) in my panGesture delegate, detect if it is a horizontal scroll, if it is not, ignore.

Swift answer:

let scrollViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
scrollViewPanGesture.delegate = self
scrollView.addGestureRecognizer(scrollViewPanGesture)

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!