UITapGestureRecognizer吞下所有手势

时间:2012-06-07 07:34:36

标签: ios uiview uiviewcontroller multi-touch uitapgesturerecognizer

我正在尝试将UITapGestureRecognizer添加到我的应用中,以便我可以检测并操作所有两个手指点击,但通过任何其他用户输入。听起来微不足道,但我无法让它发挥作用。到目前为止,这是我的主要视图控制器中的代码:

- (void) viewDidLoad {
  // other init stuff
  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
  [tapRecognizer setNumberOfTouchesRequired:2];
  [tapRecognizer setNumberOfTapsRequired:1];
  [tapRecognizer setCancelsTouchesInView:NO];

  UIView *tapHolderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  [tapHolderView setMultipleTouchEnabled:YES];
  [tapHolderView addGestureRecognizer:tapRecognizer];

  [self.view addSubview:tapHolderView];

  [tapRecognizer release];
}

- (void) handleTap:(UITapGestureRecognizer *) sender {
  if (sender.state == UIGestureRecognizerStateEnded) {
    // do something
  }
}

这是怎么回事:双指点按将触发handleTap方法。任何其他用户输入,无论是滑动,捏合,单指点击等,都不会被任何其他视图(UIToolbar,UIWebView等)拾取。

我非常感谢您对此有任何见解,如果我对这个问题不够具体,请告诉我。

3 个答案:

答案 0 :(得分:6)

尝试将视图控制器设置为UIGestureRecognizerDelegate的委托,并实现gestureRecognizer:shouldRecognizeSimulaneouslyWithGestureRecgonizer始终返回“YES”。例如:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

答案 1 :(得分:1)

我唯一能想到的是使用:

 [tapRecognizer setDelegate:self];

您是否在视图控制器中为手势识别器定义了任何委托方法?你会尝试评论那条线以进行快速测试吗?

答案 2 :(得分:0)

我们在同一视图中实现了单击和双击手势。希望这有帮助

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] 
                                     initWithTarget:self 
                                     action:@selector(handleSingleTapOnMainImageView:)];

[self addGestureRecognizer:singleTap];
[singleTap release];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] 
                                     initWithTarget:self 
                                     action:@selector(handleDoubleTapOnMainImageView:)];
[doubleTap setNumberOfTapsRequired:2];
[singleTap requireGestureRecognizerToFail:doubleTap];
[self addGestureRecognizer:doubleTap];
[doubleTap release];