Why does touchesBegan not fire when the number of touches changes? - iphone

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.

Related

Is there a way to programmatically trigger a touch event at a point given on iphone?

I would like to programmatically trigger a touch event at a point on the screen. Do you know how to do this?
For example, I would like to click a UIView, then it should check to see if certain conditions are right, and if they aren't, then I will call setUserInteractionEnabled to NO, and then fire another event at the same point, so that the point goes through the view and to the next. Then the user interaction will be re-enabled.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
checking conditions if conditions are right.
{[self setUserInteractionEnabled: NO];
//fire another touch event at the same point so it goes through this view
[self setUserInteractionEnabled: YES];
}
You can't operate touch event programatically, instead I have another option to suggest you that will work the same you want.
Alternative:
You can take timer, which will be called after your desired time. In that you can call a function that will contain the data that you want to include in touch event. You can check your conditions and do y our other stuff accordingly.

why are touches (set) are passed instead of a touch in touchesBegan?

- (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.

what after only touches Began?

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.

Iphone multitouch handling

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.

MkMapView setRegion animation prevents touch events on Annotation Views

We have a MKmapView with a bunch of Image Annotation where each Image annotation responds to touch
by overriding these methods of AnnotationView subclass:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Our map region is updated using
[MkMapView setRegion:animated:]
whenever the new location is received and is far enough from the old location to make a difference.
What I noticed is that if we set animated flag to YES the touches on our annotation are rarely detected(probably due to the fact that main thread is busy animating between two map regions.
When we set animated flag to NO, everything is fine, but map transition may(or may not) become jerky.
The question I have is whether this is an expected behavior of animated flag of [MkMapView setRegion:animated] function or whether there is a workaround for this issue.
Thanks in advance
Typically, the mapview being animated has its userInteractionEnabled property set to no during the animation. If you need to change this behavior, you should subclass the view and override the appropriate methods.
Alternatively, you could place a transparent view over the mapview for the duration of the animation to capture specific types of actions, such as respond to a double-tap to stop the change in region.