1

我在我的应用程序中添加了一个 UITapGestureRecognizer 来检测双击手势。

UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc]
                                        initWithTarget:self action:@selector(handleTapGesture:)];
  tapgr.numberOfTaps = 2;
  tapgr.delegate = self;
  [_view addGestureRecognizer:tapgr];
  [tapgr release];

除非我在我的应用程序中显示工具提示,否则这工作正常。它们是这样设置的:

[_view.toolTipView addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];

在引入我的手势识别器之前,这些工具提示可以点击并做出反应,现在它们不再有反应了......

如何使手势识别器和标准 UIControlEventTouchUpInside-Setup 一起工作?

4

3 回答 3

1

我找到了一个解决方案:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
 if (gestureRecognizer == tapgr) {
   return ![touch.view isKindOfClass:[UIControl class]];
 }
 return YES;
}

此方法可防止在按下 UIControl(即 Button)时触发 GestureRecognizer。

于 2013-06-21T07:10:52.250 回答
0

如果你想让 _view 可以同时响应单击和双击,你可以试试这个

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

   [doubleTap setNumberOfTapsRequired:2];

   //Add this code to disable double tap and only responds to single tap when user tap the _view just once time
   [singleTap requireGestureRecognizerToFail:doubleTap]; 

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

如果你想使用 TouchEvent 试试 UIControlEventTouchDownRepeat

  [button addTarget:self actionselector(multipleTap:withEvent:) forControlEvents:UIControlEventTouchDownRepeat];


  //Try to check wheather is doulbe tap or single tap.

  -(IBAction)multipleTap:(id)sender withEvent:(UIEvent*)event {

     UITouch* touch = [[event allTouches] anyObject];

     if (touch.tapCount == 2) {

     // do action.

     }

  }
于 2013-06-21T06:00:38.763 回答
-1

你不能那样做。我认为你应该选择另一种方式来实现它。

于 2013-06-21T06:00:15.357 回答