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));
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:
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With