Multiple touch event in UIView
I hv the below codes in one of my UIView application.
I want to handle single touch event in touchesBegan, and multiple touch in touchesMoved.
The codes work in the simulator. But when I transfer it to an iPhone device, I find out when I touch by 2 fingers, the below codes still run occasionally, which should not be true as I hve the "if ([touches count] == 1)" statement. "OCCASIONALLY" means the "if" statement works sometimes, but not always.
Is it due to the fact that 2 fingers are not touching the screen at the same time, and say 0.1 sec behind another, so the event is triggered as 1 + 1 finger than 2 fingers at the same time??
Any one can help?
- (void) touchesBegan: (NSSet * ) touches withEvent: (UIEvent * ) event
{
// handle only 1 finger
if ([touches count] == 1)
{
NSLog(@"touchesBegan touches = 1");
pt = [[touches anyObject] locationInView:self];
[se开发者_开发百科lf setNeedsDisplay];
}
}
It might be easier to create gesture recognizers for this. One that detects 1 finger touch and one that detects 2 finger movement. These should be easily created using the UIGestureRecognizer as base class. You could then make the 1 finger touch be dependent on the 2 finger movement failing, which means that in order to trigger a 1 finger touch event the 2 finger movement gesture recognizer has to fail.
You could do that by implementing the following method:
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
Gesture recognizers are incredibly powerful stuff, I recommend you use them!
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
精彩评论