简体   繁体   中英

How to Zoom in/out an UIView based on Pan gesture?

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM