Question

I would like to Zoom in/out an UIView based on the UIPanGesture. I don't know how to convert translationInView to Scaling parameter. (I know how to zoom in/out based on pinch gesture).

I'm trying to figure out how to do this, but no such luck so far.

Was it helpful?

Solution

To make zoom you need UIPinchGestureRecognizer, not Pan

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {    
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;    
}

Also check this link.

UPDATE:

The only way to receive touches information from UIPanGestureRecognizer is

- (NSUInteger)numberOfTouches;
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;

so maybe you can try something like this:

    CGFloat old_distance = 0.0; // keep it somewhere between touches!

- (void)panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.numberOfTouches == 2) {
        CGPoint a = [recognizer locationOfTouch:0 inView:recognizer.view];
        CGPoint b = [recognizer locationOfTouch:1 inView:recognizer.view];
        CGFloat xDist = (a.x - b.x);
        CGFloat yDist = (a.y - b.y);
        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

        CGFloat scale = 1;
        if (old_distance != 0) {
            scale = distance / old_distance;
        }
        old_distance = distance;
        recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top