UIGestureRecognizer和UIControlEventTouchUpInside同时工作

时间:2013-06-21 05:31:30

标签: ios view uigesturerecognizer gestures

我正在为我的应用程序添加一个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协同工作?

3 个答案:

答案 0 :(得分:1)

我找到了解决方案:

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

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

答案 1 :(得分: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.

     }

  }

答案 2 :(得分:-1)

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