UIPinchGestureRecognizer 比例值是如何确定的?

发布于 2024-12-13 00:25:49 字数 245 浏览 0 评论 0原文

我想知道是否有人知道 UIPinchGestureRecognizer 比例值是如何确定的,或者是否有一个公式可以用来计算新的比例值?

我有一个应用程序,将 UIPinchGestureRecognizer 附加到 imageView,在某些特定情况下,如果缩小 imageView,我需要手动重新调整比例code> 因此它会经过屏幕的某个点。谢谢。

I was wondering if anyone knows how the UIPinchGestureRecognizer scale value is determined, or if there is a formula I could use to calculate a new scale value?

I have an app where I attach a UIPinchGestureRecognizer to an imageView, and in certain specific instances, I need to manually readjust a scale if it shrinks the imageView so it goes past a certain point of the screen. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-12-20 00:25:49

给定两个初始点(触摸),使用毕达哥拉斯定理计算它们之间的距离。将此距离称为“初始距离”。

对于点的每次连续更新,重新计算点之间的距离,并将该距离称为“新距离”

scale = "new distance" / "initial distance". 

如果有人实际上不知道......毕达哥拉斯定理是:

sqrtf(powf(bx - ax, 2.0f) + powf(by - ay, 2.0f))

理解缩放很简单公式...如果您的手指间距是开始捏合时间距的两倍,则缩放应为 2.0 (2x) - 所以插入一些数字...最初间距 50px...现在相距 100px = 100 / 50 = 2

Given two initial points (touches), compute the distance between them using pythagorean theorem. Let this be distance be called the "initial distance".

For each successive update of the points, recompute the distance between the points and let this distance be called the "new distance".

scale = "new distance" / "initial distance". 

In case anyone doesn't actually know... pythagorean theorem is:

sqrtf(powf(b.x - a.x, 2.0f) + powf(b.y - a.y, 2.0f))

It's simple to understand the scaling formula... if your fingers are twice as far apart as they were when you started pinching, the zoom should be 2.0 (2x) - so plug in some numbers... 50px apart initially... 100px apart now = 100 / 50 = 2

请爱~陌生人 2024-12-20 00:25:49

我通过做一些逆向工程找到了我需要的东西。正如你们大多数人都知道谁已经使用 UIPinchGestureRecognizer 实现了缩放方法一样,您最终会得到这行代码:

GFloat nextScale = 1 -  (previousScale - [sender scale]);

这是根据 UIPinchGestureRecognizer 给出的值计算的 nextScale,但是我需要制作自己的自定义缩放,因为用户已经超出了我设定的界限。因此,我确定了需要将 imageview 缩小到的宽度,并通过将其设置为比例来确定所需的 adjustmentScale:(nextScale / adjustmentScale) = (nextWidth / adjustmentWidth)。

然后,我将 adjustmentScale 重新插入到上述公式中,以确定我的新规模金额的 [发送方规模]:[发送方规模] = AdjustedScale - 1 - previousScale。

所以现在我可以使用该金额来设置下次需要的 previousScale 值。

I found out what I needed by doing a little reverse engineering. As most of you know who have implemented a scale method with the UIPinchGestureRecognizer, you ultimately end up with this line of code:

GFloat nextScale = 1 -  (previousScale - [sender scale]);

This is the nextScale calculated on the value given by the UIPinchGestureRecognizer, however I needed to make my own custom scale since the user had scaled past the boundary I set up. So I determined the width I needed my imageview to be shrunk to, and determined the adjustedScale I needed by setting it up as a proportion: (nextScale / adjustedScale) = (nextWidth / adjustedWidth).

Then I reinserted the adjustedScale into the above formula to determine what the [sender scale] would be for my new scale amount: [sender scale] = AdjustedScale - 1 - previousScale.

So now I can use that amount to set the previousScale value which I'll need for next time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文