简体   繁体   English

iPhone:检测两个手指触摸

[英]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 ? 这很难吗?

Make sure the UIView has multipleTouchEnabled=YES , the default is NO . 确保UIView具有multipleTouchEnabled=YES ,默认为NO

Edit:I see what the problem is. 编辑:我明白了问题所在。 In touchesBegan:withEvent:, you will only get the new touches. 在touchesBegan:withEvent:中,你只会得到新的感受。 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: 要检查是否有多个活动触摸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] ;
}
- (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];
    }
}

What is the real object that is receiving the event? 接收活动的真实对象是什么? That object may get your touch event and not the base UIResponder. 该对象可能会触及您的触摸事件,而不是基础UIR应答器。

For example if you are inside a UIScroolView then you can get the sec touch in: 例如,如果您在UIScroolView中,那么您可以获得秒触摸:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM