单击和双击手势识别器

我试图添加2个 UITapGestureRecognizers到一个视图,一个为单击和一个为双击事件。单击识别器正在按预期工作(单独工作)。但我好像没办法让双击识别器工作。

已经尝试过像 cancelsTouchesInViewdelaysTouchesBegandelaysTouchesEnded这样的性质,但仍然不起作用。

当我双击时,单击识别器将始终被激活,双击事件也将被发送到超级视图。但是,自定义双击识别器似乎根本没有得到通知。

文件似乎表明,上述3个属性可用于此目的。但我只是不确定应该设置什么值以及在哪个识别器上(单个、双重或两者兼有)。希望有熟悉这个的人能帮忙。

下面是最新更新的代码块。

// ****** gesture recognizers ******


- (void)addSingleAndDoubleTapGestureRecognizersToView:(UIView *)view
{
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer: singleTapRecognizer];


// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
[view addGestureRecognizer: doubleTapRecognizer];
}


- (void)handleSingleTapOnView:(id)sender
{


}


- (void)handleDoubleTapOnView:(id)sender
{


}
107235 次浏览

你需要使用必需的 GestureIdentity izerToFail: 方法:

[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];


UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];


[singleTap requireGestureRecognizerToFail:doubleTap];

注: 如果你正在使用 numberOfTouchesRequired 一定是 .numberOfTouchesRequired = 1;

为了斯威夫特

let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)


let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)


singleTapGesture.require(toFail: doubleTapGesture)

不知道这是不是你想要的,但是我没有手势识别器就敲了两下。我在 UITableView 中使用它,所以我在 did SelectRowAtIndexPath 方法中使用了该代码

    tapCount++;
switch (tapCount)
{
case 1: //single tap
[self performSelector:@selector(singleTap:) withObject: indexPath afterDelay: 0.2];
break;
case 2: //double tap
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap:) object:indexPath];
[self performSelector:@selector(doubleTap:) withObject: indexPath];
break;
default:
break;
}
if (tapCount>2) tapCount=0;

方法 singleTap 和 doubleTap 仅以 NSIndexPath 作为参数为空:

- (void)singleTap:(NSIndexPath *)indexPath {
//do your stuff for a single tap
}


- (void)doubleTap:(NSIndexPath *)indexPath {
//do your stuff for a double tap
}

希望能有帮助

//——首先您必须分配双击和单击手势——//

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


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


[singleTap requireGestureRecognizerToFail : doubleTap];
[doubleTap setDelaysTouchesBegan : YES];
[singleTap setDelaysTouchesBegan : YES];

//————————————数量——————————————//

[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];

在视图或按钮上添加双击和单击手势——//

[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];

有些视图有内置的双击识别器(例如 MKMapView)。为了解决这个问题,你需要实现 UIGestureRecognizerDelegate方法 shouldRecognizeSimultaneouslyWithGestureRecognizer并返回 YES:

首先实现你的双重和单一识别器:

// setup gesture recognizers
UITapGestureRecognizer* singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(mapViewTapped:)];
singleTapRecognizer.delegate = self;
singleTapRecognizer.numberOfTapsRequired = 1;




UITapGestureRecognizer* doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(mapViewDoubleTapped:)];
doubleTapRecognizer.delegate = self;    // this allows
doubleTapRecognizer.numberOfTapsRequired = 2;
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];

然后执行:

#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer {  return YES; }

关于@stanley 的评论-

不要尝试和使用自来水手势排选择工作在 UITableView,因为它已经有完整的自来水处理..。

但是你必须在你的单击手势识别器上将“取消触摸视图”设置为“否”,否则它将永远不会得到点击事件。

Swift 2的解决方案:

let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTapGesture.numberOfTapsRequired = 1 // Optional for single tap
view.addGestureRecognizer(singleTapGesture)


let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)


singleTapGesture.requireGestureRecognizerToFail(doubleTapGesture)

迅捷3解决方案:

let singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
singleTap.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTap)


let doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
doubleTap.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTap)


singleTap.require(toFail: doubleTap)

在代码行 < em > singleTap.demand (toFail: doubleTap) 中,我们强制单击等待,并确保单击事件不是双击。

我实现了 UIGestureIdentiizerGenerate 方法来检测 singleTap 和 doubleTap。

照我说的做。

 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapGesture:)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
[self.headerView addGestureRecognizer:doubleTap];


UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[singleTap setDelegate:self];
[doubleTap setDelaysTouchesBegan:YES];
[singleTap setDelaysTouchesBegan:YES];
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.headerView addGestureRecognizer:singleTap];

然后实现这些委托方法。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return  YES;
}


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