Horizontal UIScrollView having vertical UIScrollViews inside - how to prevent scrolling of inner scroll views when scrolling outer horizontal view?

后端 未结 6 901
失恋的感觉
失恋的感觉 2021-02-09 18:50

couldn\'t find a solution for that.

I am building an app, with big scroll view, who has paging (Horizontal). Inside this scroll view, there is a grid of UIView\'s, and a

6条回答
  •  梦如初夏
    2021-02-09 19:03

    You could subclass UIScrollView and implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:, allowing the scroll view's built-in panGestureRecognizer to recognize simultaneously with another scroll view's gesture recognizers.

    Example:

    //This is needed because the public UIScrollView headers
    //don't declare the UIGestureRecognizerDelegate protocol:
    @interface UIScrollView (GestureRecognition) 
    @end
    
    @interface MyScrollView : UIScrollView
    
    @end
    
    @implementation MyScrollView
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        if (gestureRecognizer == self.panGestureRecognizer) {
            return YES;
        } else if ([UIScrollView instancesRespondToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]) {
            //Note: UIScrollView currently doesn't implement this method, but this may change...
            return [super gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
        }
        return NO; //the default
    }
    
    @end
    

    Using this subclass for either the horizontal, or all vertical scroll views should suffice.

    You might actually find that you like the default behavior better, after you've tried using it this way. Allowing both views to scroll simultaneously will almost always lead to accidental vertical scrolling while swiping left and right, which can be irritating (most people don't do a perfectly horizontal swipe gesture).

提交回复
热议问题