iPhone:检测两个手指触摸

时间:2011-02-03 20:10:03

标签: 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) ;   
  }
}   

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

3 个答案:

答案 0 :(得分: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] ;
}

答案 1 :(得分:0)

接收活动的真实对象是什么?该对象可能会触及您的触摸事件,而不是基础UIR应答器。

例如,如果您在UIScroolView中,那么您可以获得秒触摸:
  - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

答案 2 :(得分:0)

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