如何使用UIPinchGestureRecognizer设置最小和最大缩放比例 [英] How to set minimum and maximum zoom scale using UIPinchGestureRecognizer

查看:793
本文介绍了如何使用UIPinchGestureRecognizer设置最小和最大缩放比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想放大并缩小图像视图,我不想使用UIScrollView。
所以我使用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;

这适用于放大和缩小。
但问题是我想放大和缩小特定比例,如在UIScrollView中我们可以设置maxZoom和minZoom。我找不到任何解决方案,每个关于UIPinchGestureRecognizer的教程都只描述相同的代码。

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.

推荐答案

声明2个ivars CGFloat __scale CGFloat __previousScale 在处理手势的类的界面中。通过覆盖其中一个 init 函数,将 __ scale 设置为 1.0 (确保在这里调用超级构造函数。)

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

这篇关于如何使用UIPinchGestureRecognizer设置最小和最大缩放比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆