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

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

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

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

Related

restart an uitouch

I've tried all morning, and start to think it's impossible.
So you have a touch in your screen, it entered the response chain and maybe did something. Then you moved it on the screen it called the touchesMoved: method (of the uiResponder associated with the touch) and maybe did other stuff.
At this point, is there anyway, that the touch continues to move in the screen, and when it enters some other uiView, the uiTouch gets reinitialized (I mean, call touchEnded, in the first uiView, then in the new uiView call touchesBegan, and then if continues movement call touchesMoved).
I've tried to resignFirstResponder and to manually call the methods, and I just can't.
I keep thinking that there should exist a really simple answer for this. Something like
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
if (!CGRectContainsPoint(self.frame, location)) {
[touch restart];
}
or
[[touch phase] restart];
(But anything in documentation ):
My recommendation would be to have the superview of all the views you want to transition through to receive the touches, then do a hit test on the subviews within the touchesMoved/Began/Ended methods (probably via CGRectContainsPoint(...)), and pass the touches along accordingly (calling the corresponding touchesBegan/Moved/Ended method on the subview).

difference between touchMoved and Swipe?

i am rotating circle in iPad.i have inserted swipegesture event.but I want to different operations in touchMoved and swipeEvent.but when I do touch moving , swipw gesture is called, what i have to do , any help please?
swipe:
NSEventTypeSwipe
An event representing a swipe gesture.
Available in Mac OS X v10.6 and later.
Declared in NSEvent.h.
and
touchMoved:
Sent to the receiver when one or more fingers move in the associated view.
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
so swipe cant be use to code some thing when any thing happens like touches.swipe is use for recognizing touch event.

How are objects made draggable in iphone app?

I would like to have objects in my view which can be dragged and dropped.
When they are dropped in a certain area they should disappear and some code should execute.
How are objects made draggable in an iphone app? Is it just a button which can be drag enabled?
How would one detect the position of the draggable object? Would it be as simple as (Psuedo)
if (draggableButton1.xPosition > e.g. && draggableButton1.xPosition < e,g
&& draggableButton1.yPosition > e.g. && draggableButton1.yPosition < e,g) {
do something
}
?
When user touches the object that time following method is called.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
When moved with touched, following method will be called.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
When user end with touch, the following method will be used
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
The NSSet object contain the UITouch object which contain all information for touches on view, which view, position on screen , previous position and so more. You can check documentation for that.
Start your logic from touchesBegan and change the position of your object in touchesMoved event. then when user drop object, touchesEnded event will be called. Check for position in touchesEnded event and excute your code as you have said.
May this three methods will help...
If said object is an UIView you can get its frame origin and size.
Check Apples MoveMe example: http://developer.apple.com/iphone/library/samplecode/MoveMe/Introduction/Intro.html

how to implement two fingers panning like safari broswer?

i try to implement panning and zooming functionality like safari browser in ipad.
i used UIPinchGestureRecognizer for zooming with two fingers touch. but i dont know how to implement two fingers panning.
when i touch with two fingers its tap count is 1.
please help.
thanks in advance.
You don't want the tapCount, you want the number of touches. If you touch down with two fingers you can two touch events each with a tap count of 1.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[touches count] would return 2, one for each finger tip.
Read through the apple guide for touch events

What happens when dragging off screen?

I have an object that can be dragged around. Once the user's finger goes off screen and comes back, I loose the ability to drag the object. If the user then does another touch and drag, everything is fine.
How can I get notified once the user's finger drags back onto the screen? Since touchesBegan doesn't fire, I don't get any notification.
Here is my touchesMoved, which I call in the touchesBegan:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
//stop object dragging at edge of screen
if(location.x > 35){
myboject.center = location;}
}
The described behaviour seems normal to me, and all built-in Apple apps behave the same way. Since there’s no touch screen outside of the touch screen (yep), I believe there’s no way the device can distinguish between touch beginning or moving from outside of the screen.