限制在IOS中使用捏合手势缩放图像的最大比例 [英] Limit maximum scale for scaling image with pinch gesture in IOS

查看:471
本文介绍了限制在IOS中使用捏合手势缩放图像的最大比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前能够使用Apple提供的捏合手势识别器进行图像缩放,但是如何限制图像的最大比例?

I am currently able to make an image scale by using the pinch gesture recognizer provided by Apple, but how can I limit the maximum scale for the image?

I一直在使用来自Apple的SimpleGestureRecognizers示例,它将比例设置为1,并且可以非常轻松地缩放图像,但是当我删除它时,它会突然跳转,这不太好。但是很难跟踪比例,因为它总是设置为1.

I have been using the SimpleGestureRecognizers sample from Apple, which sets the scale to 1, and makes it very easy to scale image, but when I remove it, it jumps suddenly, which is not nice. But then it is hard to track the scale since its alway set to 1.

提前谢谢你。

推荐答案

UIKit会针对单个用户手势多次调用您的选择器方法(捏合。)每个调用将代表比例中的小增量(或减少)。如果您处理每个方法调用并相应地为每个小增量缩放图像,您将获得平滑的动画。

UIKit will call your selector method many times for a single user gesture (pinch.) Each call will represent a small increment (or decrease) in the scale. If you handle each call to the method and scale your image correspondingly for each small increment, you will get a smooth animation.

您可以使用识别器的scale属性来修改图像。然后,将属性重置为1.0,以便下次调用selector方法时,scale属性仅表示自上一个以来的新的小增量。您再次按新的小增量重新缩放图像并重置为1.0。如果您没有重置识别器的比例并使用它来重新缩放图像,您将按累积变化缩放图像,从而导致跳跃行为(以及不正确的缩放。)

You use the recognizer's scale property to modify your image. Then, you reset the property to 1.0 so that the next time the selector method is called, the scale property represents only the new small increment since the last one. You again re-scale your image by the new small increment and reset to 1.0. If you do not reset the recognizer's scale and use it to re-scale your image, you will be scaling your image by the cumulative change, resulting in jumpy behavior (and incorrect scaling.)

因此,请务必将手势识别器的比例重置为1.要跟踪比例的总变化,您可以为UIImageView或ViewController创建一个实例变量,可能也称为scale。 (事实上​​,如果你在drawRect中使用新的缩放变量来绘制图像,你只需要在setter中添加对setNeedsDisplay的调用。当缩放变化时你不必编写任何代码来重绘图像。)

So, do make sure to be resetting the gesture recognizer's scale to 1. To keep track of the total change in scale, you can create an instance variable, possibly also called 'scale', for your UIImageView or your ViewController. (In fact, if you used that new scale variable in drawRect to draw your image, you will only have to add a call to setNeedsDisplay in the setter. And you won't have to write any code to redraw the image when the scale changes.)

在任何情况下,以下代码(包含Ravin的限制检查,但在新的比例变量上)应适用于您的选择器方法:

In any case, the following code (incorporating Ravin's limit check, but on the new scale variable) should work for your selector method:

- (void)pinch:(UIPinchGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateChanged ||
        recognizer.state == UIGestureRecognizerStateEnded) {
        if (imageView.scale < yourScaleLimit) {
            imageView.scale *= recognizer.scale;
            //any other code to scale up the image if
            //just changing imageView.Scale is not enough
        }
        recognizer.scale = 1.0;
    }
}

我希望这有用。

这篇关于限制在IOS中使用捏合手势缩放图像的最大比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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