IPhone: two-finger touch detection

I need to detect an event with two fingers. If I touch the screen with two fingers at the same time, then everything is in order. Just using a code like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[touches allObjects] objectAtIndex:0]; CGPoint point1 = [touch locationInView:self]; CGPoint point2 ; NSLog(@"First: %f %f", point1.x, point1.y) ; if ([[touches allObjects] count] > 1) { UITouch *touch2 = [[touches allObjects] objectAtIndex:1]; point2 = [touch2 locationInView:self]; NSLog(@"Second: %f %f", point2.x, point2.y) ; } } 

But this code does not work if I hold one finger and then touch the screen with the other finger. How to implement this? Is it hard to do?

+8
objective-c iphone
source share
3 answers

Make sure UIView has multipleTouchEnabled=YES , the default is NO .

Edit: I see what the problem is. In Began: withEvent: contacts you get only new touches. You do not get all the active touches. It is very unlikely, if at all possible, that you will get more than one touch to start at the same time. To check if there is more than one active touch int touchhesBegan: try the following:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([[event touchesForView:self] count] > 1) { NSLog(@"%d active touches",[[event touchesForView:self.view] count]) ; } [super touchesBegan:touches withEvent:event] ; } 
+19
source share

What is the real object that receives the event? This object can receive your touch event, not the base UIResponder.

For example, if you are inside a UIScroolView, then you can get a touch screen in:
- (UIView *) viewForZoomingInScrollView: (UIScrollView *) scrollView;

0
source share
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch1, *touch2; CGPoint location1, location2; NSSet *allTouches = [event allTouches]; touch1 = [[allTouches allObjects] objectAtIndex:0]; location1 = [touch1 locationInView:self]; if([allTouches count] > 1) { touch2 = [[allTouches allObjects] objectAtIndex:1]; location2 = [touch2 locationInView:self]; } } 
0
source share

All Articles