1

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.

Joe
  • 854
  • 3
  • 11
  • 21

1 Answers1

0

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.

Javier Quevedo
  • 2,066
  • 1
  • 17
  • 27
  • Ok, thanks. One thing I've noticed is that when it does switch to the pan gesture once one finger is removed, then both gesture recognizers become active, the pan and the pinch. I think I can work around this.. But is there away to completely abandon the pinch gesture recognizer once one finger is removed? Looking at the UIGestureRecognizer documentation I'm guessing you can't. – Joe Apr 19 '13 at 02:31
  • Also one more thing.. When allowing simultaneous gesture recognition the panGestureRecognizer calls it's method, but so does the view's superview's panGestureRecognizer. It seems a bit strange to me, I don't really understand the logic behind that. I'm assuming there is no way to prevent this either? – Joe Apr 19 '13 at 02:46
  • You can prevent a gesture regoznier from being triggered by removing its target ( - (void)removeTarget:(id)target action:(SEL)action ) or by simply disabling it ( http://stackoverflow.com/questions/6593772/how-to-cancel-reset-an-uigesturerecognizer ). So whenever you want either of the recognizers to stop, simply disable it. I've never experienced your second issue (the one about the parents view). Maybe you can share some of the code you use to create the gesture recognizer to see what may be happening. Good luck. – Javier Quevedo Apr 19 '13 at 09:19