iphone - iphone - How to determine x&y of last touch in multitouch scenario? - answerstu - answerstu.com answerstu

iphone - How to determine x&y of last touch in multitouch scenario?

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

  1. Paul- Reply

    2019-11-13

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

  2. Patrick- Reply

    2019-11-13

    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
            // ...
        }
    }  
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>