Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set minimum and maximum zoom scale using UIPinchGestureRecognizer

I want to zoom in and zoom out an image view and i dont want to use UIScrollView for that. so for this i used UIPinchGestureRecognizer and here is my code -

[recognizer view].transform = CGAffineTransformScale([[recognizer view] transform], [recognizer scale], [recognizer scale]);
recognizer.scale = 1;

this is working fine for zoom in and zoom out. But problem is that i want to zoom in and zoom out in specific scale like in UIScrollView we can set the maxZoom and minZoom. i could not found any solution for that, every tutorial about UIPinchGestureRecognizer just describe the same code.

like image 584
TheTiger Avatar asked Jun 09 '12 05:06

TheTiger


3 Answers

Declare 2 ivars CGFloat __scale and CGFloat __previousScale in the interface of the class that handles the gesture. Set __scale to 1.0 by overriding one of the init functions (make sure to call the super constructor here).

- (void)zoom:(UIPinchGestureRecognizer *)gesture { 
    NSLog(@"Scale: %f", [gesture scale]);

    if ([gesture state] == UIGestureRecognizerStateBegan) {
        __previousScale = __scale;
    }

    CGFloat currentScale = MAX(MIN([gesture scale] * __scale, MAX_SCALE), MIN_SCALE);  
    CGFloat scaleStep = currentScale / __previousScale;
    [self.view setTransform: CGAffineTransformScale(self.view.transform, scaleStep, scaleStep)];

    __previousScale = currentScale;

    if ([gesture state] == UIGestureRecognizerStateEnded || 
        [gesture state] == UIGestureRecognizerStateCancelled ||
        [gesture state] == UIGestureRecognizerStateFailed) {
        // Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
        __scale = currentScale;
        NSLog(@"Final scale: %f", __scale);
    }
}
like image 159
nhahtdh Avatar answered Oct 10 '22 02:10

nhahtdh


 – (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer 
    {
        if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = [gestureRecognizer scale];
        }

        if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

        CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@”transform.scale”] floatValue];

        // Constants to adjust the max/min values of zoom
        const CGFloat kMaxScale = 2.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = 1 –  (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
        [gestureRecognizer view].transform = transform;

        lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call
        }

    }
like image 45
Kaushik Movaliya Avatar answered Oct 10 '22 01:10

Kaushik Movaliya


I had similar situation. My requirement was imageView will bounce back to its last transformation if imageView is smaller than a minimum size or bigger than a certain maximum size.

if ((self.frame.size.width > IMAGE_MIN_SIZE) && (self.frame.size.height > IMAGE_MIN_SIZE) && (self.frame.size.width < IMAGE_MAX_SIZE) && (self.frame.size.height < IMAGE_MAX_SIZE)) {
    lastSizeTransform = self.transform;
}else {
    self.transform = lastSizeTransform;
}

Here self is the imageView.

like image 28
Warif Akhand Rishi Avatar answered Oct 10 '22 01:10

Warif Akhand Rishi