3

I'm new to this site and to iOS programming.

I am working on a percussion app. For this I want to know the x and y location of every finger that touches the screen. I thought this was straightforward, but multitouch is making things confusing for me.

Suppose the user has two fingers pressed on the screen and the user presses a third finger on the screen. How do I determine the location of this third finger?

My feeling is that I need to implement touchesBegan

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

To determine the x and y location I have to look at the touch that triggered this call to touchesBegan. But the touches are presented in an unordered set. If the third finger triggered this touchesBegan, then I have three touches in the NSSet. But since the set is unordered, how do I determine the touch that triggered this third call to touchesBegan? If I understand my documentation correctly it could be any of those three touches.

Many thanks in advance

2 Answers 2

0

Maybe you can add a simple counter property and increase its value in touchesBegan and decrease in touchesEnd.

2
  • Thank you for your reply. I am not sure if I follow you. On each call of touchesBegan I can determine the number of fingers currently on the device by looking at [touches count] My problem is ; suppose the count indicates that there are three fingers on the device, and the NSSet therefore contains three UITouch objects, how do I determine which of those UITouch objects represents the latest finger that touched the screen? I want to check out the position of that UITouch object using locationInView. Nov 22, 2011 at 15:08
  • 1
    Sorry I misunderstood your goal. The UITouch has a property named timestamp. Iterating through the NSSet checking this attribute should give you the touch you are looking for.
    – Coli88
    Nov 22, 2011 at 15:52
0

Okay, it now turns out I have been mis-interpreting my test-data. If two fingers already touch the device when a third finger touches the device, only one UITouch object is part of the NSSet in the call to touchesBegan, and not three as I seemed to experience. This one UITouch represents the last fingertouch.

The only time when more than one UITouch object is passed to touchesBegan is when in fact multiple fingers begin to touch the device at the same time.

Since, in my case, I need to handle all new touches based on their location, I need to handle all UITouch objects in the NSSet.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for ( UITouch *touch in touches)
    {
        CGPoint location = [touch locationInView:self.view];
        // Handle finger touch at given location
        // ...
    }
}  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.