UIPinchGestureRecognizer 比例值是如何确定的?
我想知道是否有人知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定两个初始点(触摸),使用毕达哥拉斯定理计算它们之间的距离。将此距离称为“初始距离”。
对于点的每次连续更新,重新计算点之间的距离,并将该距离称为
“新距离”
。如果有人实际上不知道......毕达哥拉斯定理是:
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"
.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
我通过做一些逆向工程找到了我需要的东西。正如你们大多数人都知道谁已经使用 UIPinchGestureRecognizer 实现了缩放方法一样,您最终会得到这行代码:
这是根据 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:
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.