Horizontal UISwipeGestureRecognizer in subview of UIScrollView ? (UIScrollView only needs to recognize vertical scrolling)

岁酱吖の 提交于 2020-01-13 05:45:08

问题


I have a UIScrollView where I added a subview. The scrollview scrolls fine vertically and that is all it should do. I would now like to recognize left/right swipes in the subview with the help of a UISwipeGestureRecognizer. I know it is possible, but I have not come across a solution and several tries have been unsuccessful.


回答1:


Try these:

  • Set the delegate of your UIGestureRecognizer and

  • Implement shouldRecognizeSimultaneouslyWithGestureRecognizer:

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

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
     return YES;
    }
    

Hope this helps




回答2:


I was able to accomplish something related (adding a two-finger swipe gesture recognizer directly on the scroll view) with the new iOS 7 UIGestureRecognizerDelegate method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
        shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return otherGestureRecognizer == scrollView.panGestureRecognizer;
}

However, the results were not perfect - the delay waiting for the swipe gesture recognizer to fail first causes a lag in the scrollview's pan gesture recognizer getting its touches, so normal scrolling is noticeably delayed when you start to scroll.



来源:https://stackoverflow.com/questions/14054403/horizontal-uiswipegesturerecognizer-in-subview-of-uiscrollview-uiscrollview-o

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