How to set minimum and maximum zoom scale using UIPinchGestureRecognizer

混江龙づ霸主 提交于 2019-12-06 03:52:28

问题


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.


回答1:


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



回答2:


 – (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
        }

    }



回答3:


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.




回答4:


If you logged view.transform while you are pinching, you can see your image coordination which zoomed in and out. So, this solutions doesn't work as i expect. I made my solution like that;

Obj-C Version

    - (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer {

        [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, 
recognizer.scale, recognizer.scale)];

        if (recognizer.view.transform.a > 1.6) {

            CGAffineTransform fooTransform = recognizer.view.transform;
            fooTransform.a = 1.6; // this is x coordinate
            fooTransform.d = 1.6; // this is y coordinate
            recognizer.view.transform = fooTransform;
        }

        if (recognizer.view.transform.a < 0.95) {

            CGAffineTransform fooTransform = recognizer.view.transform;
            fooTransform.a = 0.95; // this is x coordinate
            fooTransform.d = 0.95; // this is y coordinate
            recognizer.view.transform = fooTransform;
        }
        recognizer.scale = 1.0;
    }

Swift Version

func handlePinchGesture(recognizer: UIPinchGestureRecognizer) {

        if let view = recognizer.view {
            view.transform = CGAffineTransformScale(view.transform,
                recognizer.scale, recognizer.scale)
            if CGFloat(view.transform.a) > 1.6 {
                view.transform.a = 1.6 // this is x coordinate
                view.transform.d = 1.6 // this is x coordinate
            }
            if CGFloat(view.transform.d) < 0.95 {
                view.transform.a = 0.95 // this is x coordinate
                view.transform.d = 0.95 // this is x coordinate
            }
            recognizer.scale = 1
        }
    }


来源:https://stackoverflow.com/questions/10958624/how-to-set-minimum-and-maximum-zoom-scale-using-uipinchgesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!