kesrut kesrut - 4 months ago 11
Objective-C Question

iPhone: detect two finger touch

I need to detect two finger touch event. If i touch the screen with two fingers on same time, so everthing is ok. Just using such code:

- (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 don't working if i hold one finger and then touch the screen with another finger. How to implement this ? Is it hard to do ?

Answer

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

Edit:I see what the problem is. In touchesBegan:withEvent:, you will only get the new touches. You don't get all the active touches. It is very unlikely, if at all possible, that you will get more than one touch to begin at the same time. To check is there is more than one active touch int touchesBegan: try this:

- (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] ;
}