iPhone:检测两个手指触摸

kes*_*rut 8 iphone objective-c

我需要检测两个手指触摸事件.如果我同时用两根手指触摸屏幕,那么就可以了.只是使用这样的代码:

- (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) ;   
  }
}   
Run Code Online (Sandbox Code Playgroud)

但是如果我握住一根手指然后用另一根手指触摸屏幕,则此代码无效.怎么实现这个?这很难吗?

fsa*_*int 19

确保UIView具有multipleTouchEnabled=YES,默认为NO.

编辑:我明白了问题所在.在touchesBegan:withEvent:中,你只会得到新的触动.你没有得到所有积极的接触.如果可能的话,不太可能在同一时间开始多次触摸.要检查是否有多个活动触摸int touchesBegan:试试这个:

- (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] ;
}
Run Code Online (Sandbox Code Playgroud)