如何在iPhone上进行捏合手势?

发布于 2024-07-26 19:34:04 字数 94 浏览 6 评论 0原文

如何在 iPhone 上实现捏合手势? 我熟悉在 iPhone 上使用触摸事件,但我非常懒,不想为像捏手势这样广泛使用的东西重新发明轮子......源代码或链接会很有帮助。

How does one implement pinch gestures on the iPhone? I'm familiar with using touch events on the iPhone but I'm extremely lazy and don't want to re-invent the wheel for something as widespread as PINCH gestures...Source code or links thereto would be helpful.

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

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

发布评论

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

评论(4

苦笑流年记忆 2024-08-02 19:34:04

您需要使用一些基本数学知识自己实现它。

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

这些是您需要在应用程序中实现的委托方法。

您需要获取两次触摸之间的距离,然后在您自己的代码中计算距离的变化。

用于获取距离的数学方程称为 点积

我只想指出他们在引用的教程中使用的方程是不正确的。
我已对其进行了更新,以包含教程中遗漏的绝对值。

这是点积:

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
    float xDist = fromPoint.x - toPoint.x;
    float yDist = fromPoint.y - toPoint.y;

    float result = sqrt( pow(xDist,2) + pow(yDist,2) );
    return result;
}

编辑:
我在之前的答案中犯了一个错误……我已经有一段时间没有做过此类数学运算了。

如果不对结果求平方根,则无法获得点之间的真实距离。 这称为计算两个向量之间的幅度。

这是正确的做法。 如果你想省略 sqrt,你将无法得到两点之间的精确距离。

如果您不计算答案的平方,您的代码将有效,但它将使用比实际值大得多的测量值。 因此,将来如果您需要获取两点之间的距离,它将返回类似 90000 的值,而不是实际的像素距离(300px)。

You need to implement it yourself using some basic math.

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

Those are the delegate methods that you need to implement in your application.

You need to get the distance between the two touches, then calculate the changes in distance in your own code.

The mathematical equation you use to get the distance is called the dot product

I just want to point out that the equation they use in the cited tutorial is incorrect.
I have updated it to include the absolute value that is missed in the tutorial.

This is the dot product:

- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
    float xDist = fromPoint.x - toPoint.x;
    float yDist = fromPoint.y - toPoint.y;

    float result = sqrt( pow(xDist,2) + pow(yDist,2) );
    return result;
}

Edit:
I made a mistake in my previous answer....it's been a while since I've done any of that kind of math.

If you don't square root the result, you won't get the true distance between the points. This is called calculating the magnitude between two vectors.

This is the proper way to do it. If you want to omit the sqrt, you will not have the exact distance between the two points.

If you don't square the answer, your code will work but it will be using measurements that are much larger than the actual values. So in the future if you need to get the distance between the two points, it will return something like 90000 instead of the actual pixel distance which would be 300px.

琴流音 2024-08-02 19:34:04

可以肯定的是,这里提出的解决方案已经过时,有利于在 iOS 3.2 及更高版本中使用新的 UIPinchGestureRecognizer 类。

Pretty sure the solutions presented here have been obsoleted, in favor of using the new UIPinchGestureRecognizer class in iOS 3.2 and later.

巴黎夜雨 2024-08-02 19:34:04

嗯……似乎有些人对如何使用基本数学感到非常困惑。 点积完全无关紧要,只会混淆问题。 两个向量之间的点积用于计算它们之间的角度。

您实际上需要使用毕达哥拉斯定理。 点与寻找点之间的距离无关 - 只是毕达哥拉斯!

hmmm.....it seems that some people are extremely confused on how to use basic math. the dot product is totally irrelavent and just confuses the issues. dot prod between 2 vectors is used to calculate the angle between them.

You actually need to use pythagorus theorem. Dot is NOTHING to do with finding distance between points - only pythagorous!

复古式 2024-08-02 19:34:04

如果您正在寻找捏合手势的简单代码示例,我将使用 Brock 在示例应用程序中描述的方法 我在这里描述(在该页面上应用程序的倒数第二个和最后一个版本中)。 在该示例中,捏合手势用于缩放几个核心动画层。

他所描述的距离计算只是应用于两点之间的毕达哥拉斯定理(X 和 Y 距离作为直角三角形的两个面)。

If you're looking for a simple code example of the pinch gesture, I use the method described by Brock in the example application I describe here (in the second-to-last and last versions of the application on that page). The pinch gestures are used to scale a couple of Core Animation layers in that example.

All he's describing as far as the distance calculation is simply the Pythagorean Theorem applied between two points (with the X and Y distances as the two faces of a right triangle).

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