In my iPad app...
I am doing some stuff of dragging an object....
My problem is for dragging I need two methods...
touches began some stuff1
touches moved some stuff2
Sometimes what happens that the user is not moving the object after touching.
So, whateever somestuff1 has been done.
I need to reverse it back..
So,How would I do that...
Means is there any event or notification that I can fire
if the user does the touches began and not touches ended.
You will always receive a touchesEnded:withEvent: message or a touchesCancelled:withEvent: message after you have received a touchesBegan:withEvent: message. You need to override both methods if you want to know when the user has lifted his finger.
If you want to track whether the user moved the touch before lifting his finger, you have to do that yourself. You can set a flag in your touchesMoved:withEvent: method, or you can save the original position of the touch in touchesBegin:withEvent: and then compare it with the final position of the touch in touchesEnded:withEvent: and touchesCancelled:withEvent:.
You have a - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event function, too, use it to check if the user hasn't made any moves.
Related
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
why is 'touches' not just 'touch' being passed for touchesBegan/Moved/Ended?
What's the meaning of them?
That's because user may do a multiple taps (quickly) in which case you'll receive a set of multiple NSTouch objects.
A demonstration of this is the zooming effect when you double tap on a scroll (or image) view.
Because you can receive multiple touches, but they are disabled by default. In order to receive multiple touch events you must set the multipleTouchEnabled property of the corresponding view instance to YES and touch with 2 or more fingers.
I was trying to implement palm rejection functionality for a drawing app i developed for iPhone and noticed some weird behavior in touch events. When i place my palm on the screen and continuously lift some region of my hand up and then lower it down again, i get lots of touchBegin events but only a few touchEnd events. Is there something i don't know about the touch handling mechanism of iOS? Shouldn't be the number of touchEnd and touchBegin events belonging to each UITouch object equal?
There is only one view on my window and it occupies the entire screen. Both the view and the window have multitouch enabled. I'm counting the events by printing the number of touches using NSLog's at the beginning of touchBegin and touchEnd methods. So i'm taking into account the fact that a single event may contain info about multiple touches.
Don't forget to provide a handler for touchesCancelled events. You can get a touchesCancelled call after a touchesBegan and without a matching touchesEnded event.
Rather than looking at the number of touchesBegan:withEvent: and touchesEnded:withEvent: calls, you should look at the NSSet of UITouch objects passed to those methods. So, for example, if you placed one finger then a second finger on the screen, you'd get two touchesBegan:withEvent: calls. If you lifted both fingers from the screen simultaneously, you'd get a single touchesEnded:withEvent: call; the NSSet of UITouch objects passed in would indicate that two fingers were lifted.
For some reason my touchesBegan method doesn't seem to be responding correctly. If I touch the screen with two fingers, then lift one up and put it down again, touchesBegan gets called correctly. If I touch the screen with one finger, then add a second finger, touchesBegan does not get called like it should. Is there some flag that I need to check? Below is a sample that illustrates my problem:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touch");
}
Any ideas what's going on? I'm testing on an iPad with iOS 4.2 if it matters.
Yes, you need to set the multipleTouchEnabled property on your view.
When set to NO, the receiver receives
only the first touch event in a
multi-touch sequence. The default
value of this property is NO.
I'm writing an iPhone app, and I want to handle multitouches. I'm using cocos2d libs. So I've made a CCLayer subclass and set it to be a CCStandartTouchDelegate. For some reason I don't want to use UIGestureRecognizer and to build a correct logic I should know the answers for these questions:
If I tap the screen with one finger, and then with the other one. How many touches will be caught in ccTouchesBegan?
If I tap the screen with two fingers and then will move only one of them. How many touches will be caught in ccTouchesMoved?
The best thing to do when you have a question like this is just to implement the callbacks, and in the implementation, log the parameters. For example:
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Log everything (there will be repetition because the event contains the set of touches):
NSLog(#"ccTouchesBegan: touches = %#; event = %#", touches, event);
// Or, just log the number of touches to simplify the output:
NSLog(#"ccTouchesBegan: %d touches", [touches count]);
return kEventHandled;
}
Then just run your app and experiment, watching the log. You'll learn more this way (and faster) than you will by asking here.
But to answer your specific questions:
You should get one call to ccTouchesBegan for each tap (even if the first finger is still down when the second tap occurs). If the two fingers hit simultaneously, you'll get one call with two touches.
You'll get repeated calls to ccTouchesMoved each time one or more of the fingers moves. If only one finger is moving, each call will be passed a single touch. Stationary fingers will not generate events until they are moved or lifted.
Of course, remember to set isTouchEnabled = YES for your CCLayer or you won't get any callbacks at all.
Is there a way to detect a finger's non-movement by using a combination of UITouch events?
The event methods touchesEnded and touchesCancelled are only fired when the event is cancelled or the finger lifted. I would like to know when a touch has stopped moving, even while it is still touching the screen.
Simply use the following UITouch property:
UITouchPhase phase;
if its value is UITouchPhaseStationary, then the finger has not moved on the screen since the last event received. This implies that you get the related touch in
touchesBegan:withEvent:
and then the user simply does not move his/her finger.