本文介绍了我如何输出Xcode for iOS中两个触摸点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的目标:让用户触摸屏幕上的两个不同点,应用程序将输出一个代表这些点之间距离的数字。我怎样才能做到这一点?

Here is my goal: to have a user touch two different points on the screen, and the app will output a number that represents the distance between these points. How can I accomplish this?

推荐答案

在简单的层面上,您可以简单地使用勾股定理方法来计算距离两点之间。

At a simple level, you could simply use a pythagorean theorem approach as follows to calculate the distance between the two points.

double distance = sqrt(pow((x2 - x1), 2.0) + pow((y2 - y1), 2.0));

我认为你使用的是 - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件方法(注册为UIResponder并通过 [self setMultipleTouchEnabled:YES]接收多个触摸; ) ,在这种情况下,你可以提取 CGPoint .x .y 值来自NSSet并使用 locationInView 方法获取有问题的触摸的 CGPoint

I presume you're using a - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method (after registering as a UIResponder and receiving multiple touches via [self setMultipleTouchEnabled:YES];), in which case you can extract the CGPoint's .x and .y values by extracting the provided UITouches from the NSSet and using the locationInView method to obtain the CGPoint for the touch in question.

如果您之前没有使用过这些课程,我很想读起来:

If you've not used these classes before, I'd be tempted to read up on:




  • UIResponder Class Reference
  • UITouch Class Reference

但是,如果你之前还没有使用过这类东西,我d还建议阅读文档为您提供了良好的基础。 (您可能还想退后一步并使用文档,因为这些文档详细介绍了(以及源代码)如何捕获触摸事件等。)

However, if you've not yet used such things before, I'd also recommend a read of the Event Handling Guide for iOS documentation to give you a good grounding. (You might also want to take a step back and consume the Creating an iPhone Application docs, as these go into quite a bit of detail (along with source code) as to how you can capture touch events, etc.)

这篇关于我如何输出Xcode for iOS中两个触摸点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:02