Search code examples
objective-cuiscrollviewuigesturerecognizer

Disable pan in UIScrollView on pinch by 2 fingers


I want to stop scrolling after detect second touch and handle touches with my own pinch gesture. I've tryed this in scroll view:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(event.allTouches.count > 2)self.panGestureRecognizer.enabled = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(event.allTouches.count > 2)self.panGestureRecognizer.enabled = YES;
}

But it's doesnt works.

Try this:

scroll.panGestureRecognizer.maximumNumberOfTouches = 1;

But nothing


Solution

  • I find solution. I redefined UIScrollView, and add:

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

    And disable\enable pan gesture:

    if(pinch.state == UIGestureRecognizerStateBegan) scroll.panGestureRecognizer.enabled = NO; 
    if(pinch.state == UIGestureRecognizerStateEnded) scroll.panGestureRecognizer.enabled = YES; 
    

    Now my pinch gesture works.