Horizontal UISwipeGestureRecognizer in subview of

2019-04-12 22:25发布

问题:

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.