0
votes

I have attached a pan and pinch gesture to a view. I want to coordinate them such that when the each complete one cycle of "UIGestureRecognizerStateChanged" state I want to take a coordinated action that aggregates the updated information from each. Specifically, my pan controls translation and my pinch controls scale. I want to do incremental matrix concatenate of both translation and scale in a single place rather then have each concatenate autonomously which results in an unpleasant stair-step type movement.

I have stared at the UIGestureRecognizerDelegate docs but see no way to make it do what I want.

Thanks,
Doug

1

1 Answers

0
votes

The first thing to try is making sure the recognizers are working "simultaneously". In your delegate, you want to define the following:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{        
    return (gestureRecognizer.view == otherGestureRecognizer.view);
}

In my experience, that's enough to give an adequately smooth experience. However, if it's not, then what I'd do is to only update your transform from one of the recognizers, storing the state from the other in a property. Let's say that you've declared a CGFloat property called cachedScale, which you set to 1.0 in your initializer. Then in your pinch and pan handlers, you'd do the following:

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{    
    self.cachedScale *= recognizer.scale;
}

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer
{
    CGAffineTransform transform = CGAffineTransformMakeScale(self.cachedScale, self.cachedScale);
    self.cachedScale = 1.0;

    CGPoint translation = [recognizer translationInView:recognizer.view.superview];
    CGAffineTransformTranslate(transform, translation.x, translation.y);

    // do something with your transform
}

If you're just trying to drag a view around, you may have more luck changing the view's center rather than applying a translation to its transform.