I am currently using this code to get the locations of all the touches:
NSSet *allTouches = [event allTouches];
NSArray *allObjects=[allTouches allObjects];
for (int i=0;i<[allObjects count];i++)
{
UITouch *touch = [allObjects objectAtIndex:i];
CGPoint location = [touch locationInView: [touch view]];
//Add to array....
}
While testing it on the simulator (don’t have an iPad now to test it), it works perfectly with single-touch. But when trying with multiple-touches, the first iteration is correct while the second iteration doesn’t give the correct position.
i.e.
First touch: (536,163) correct
Second touch: (198,608) but should be somewhere around (148,345)
I have a feeling that I should change something with [touch locationInView: [touch view]]; to give the right location but I don’t know what to change.
Any help is appreciated.
It’s difficult to understand the issue from a set of coordinates without seeing your views, any subviews and where you’re touching in that, but you should know that:
[touch locationInView:aView]gives the coordinates of the touch in the coordinate system ofaView. That is, the coordinates you see are relative to the top left ofaViewwhich may not be what you’re expecting. Try[touch locationInView:self], which is more common.Have a look at the “Events and Touches” section of the Event Handling Guide for iOS. You generally don’t need to iterate over that collection if you’re trying to track multiple touches. iOS handles all that for you. You can use properties
tapCountandphaseto get information about whether touches have moved, how many fingers are down and so on.Does any of that help?