2

我正在尝试将 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 等)拾取。

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

4

3 回答 3

6

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
于 2012-06-07T07:58:25.743 回答
1

我唯一能想到的是使用:

 [tapRecognizer setDelegate:self];

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

于 2012-06-07T07:38:06.767 回答
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];
于 2012-06-07T07:56:45.073 回答