Search code examples
iostouchuigesturerecognizeruipangesturerecognizeruipinchgesturerecognizer

How to transition a gesture from a UIPinchGestureRecognizer handler to a UIPanGestureRecognizer handler?


I have a handler method for a UIPinchGestureRecognizer that is used to scale a view. What I'm trying to is to make it so that while two touches are on screen for the pinch gesture, and if a finger is lifted, the remaining finger would be managed by a pan gesture recognizer. Is there any way to do this? I can't think of a proper way to pass one gesture from one handler to another.


Solution

  • This should be pretty easy. Create two gesture recognisers. the UIPinchGestureRecognizer and the UIPanGestureRecognizer. Don't forget to become the delegate of both!

        pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleHandler:)];
        [pinchRecognizer setDelegate:self];
        [pinchRecognizer setDelaysTouchesBegan:NO];
        [pinchRecognizer setDelaysTouchesEnded:NO];
        [pinchRecognizer setCancelsTouchesInView:NO];
        panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
        [panRecognizer setMaximumNumberOfTouches:2];
        [panRecognizer setMinimumNumberOfTouches:1];
        [panRecognizer setDelegate:self];
        [panRecognizer setDelaysTouchesBegan:NO];
        [panRecognizer setDelaysTouchesEnded:NO];
        [panRecognizer setCancelsTouchesInView:NO];
    

    Now implement the following method of the gesture delegate.

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

    }

    This will make both handlers be triggered, so that when you release one of the fingers it will pan, but when you have both of them it will pinch.