Limit touches recognized on UIScrollView

2019-09-03 21:30发布

问题:

I'm trying to limit the number of touches recognised on a UIScrollView as it's executing with another gesture that has more than one touch required. I don't want the pan & swipe gestures to fire if the number of touches is greater than 1. But I'm having no success.

I've subclassed the UIScrollView and overridden the obvious methods, but numberOfTouches always returns 1?

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.numberOfTouches > 1) 
    {
        return NO;
    }   
    return YES;
}

回答1:

Perhaps you can do the following?

for(UIGestureRecognizer* gr in _scrollview.gestureRecognizers)
{
    if([gr respondsToSelector:@selector(setMaximumNumberOfTouches:)])
    {
        gr.maximumNumberOfTouches = 1;
    }
}

No need for sub-classing the scroll view, you can just do this in viewDidLoad of your view controller.