如何同时将 UILongPressGestureRecognizer 和 UITapGestureRecognizer 添加到同一控件?
How to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control simultaneously?
类似这个问题:
iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?
但我的问题比较复杂
我想实现您在 iPad 上的 iOS 8 中看到的相同行为。我指的是 Safari 中的页面网格。
问题:一个视图应该响应长按和点击手势识别器。以下内容应该有效:
1)关闭按钮接受点击
2)当点击开始时,所选视图应执行缩放动画
3)长按所选视图变为可拖动
如果我不使用 (requireGestureRecognizerToFail:)
,则点击手势不起作用。如果我使用这种方法,那么一切正常,但长按事件会发生巨大的延迟。
如何解决这个问题。
您需要使用requireGestureRecognizerToFail
方法。
//Single tap
UITapGestureRecognizer *tapDouble = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTapGestureForSearch:)];
tapDouble.numberOfTapsRequired = 1;
tapDouble.delegate = self;
[self addGestureRecognizer:tapDouble];
//long press
UILongPressGestureRecognizer *longPressGestureRecognizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressRecognizer:)];
longPressGestureRecognizer.numberOfTouchesRequired=1;
longPressGestureRecognizer.minimumPressDuration = 0.5f;
[longPressGestureRecognizer requireGestureRecognizerToFail:tapDouble];
longPressGestureRecognizer.delegate = self;
[self addGestureRecognizer:longPressGestureRecognizer];
这意味着长按手势等待单次点击。
您可以为长按手势添加时间。
UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(ontappLongPressGesture:)];
longPressGesture.minimumPressDuration=0.6;
longPressGesture.delegate=self;
[cell.view addGestureRecognizer:longPressGesture];
UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellSelected:)];
//[gesture requireGestureRecognizerToFail:longPressGesture];
gesture.delegate=self;
[cell.view addGestureRecognizer:gesture];
also you need to set this delegate to work both gesture together
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
类似这个问题:
iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?
但我的问题比较复杂
我想实现您在 iPad 上的 iOS 8 中看到的相同行为。我指的是 Safari 中的页面网格。
问题:一个视图应该响应长按和点击手势识别器。以下内容应该有效:
1)关闭按钮接受点击
2)当点击开始时,所选视图应执行缩放动画
3)长按所选视图变为可拖动
如果我不使用 (requireGestureRecognizerToFail:)
,则点击手势不起作用。如果我使用这种方法,那么一切正常,但长按事件会发生巨大的延迟。
如何解决这个问题。
您需要使用requireGestureRecognizerToFail
方法。
//Single tap
UITapGestureRecognizer *tapDouble = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTapGestureForSearch:)];
tapDouble.numberOfTapsRequired = 1;
tapDouble.delegate = self;
[self addGestureRecognizer:tapDouble];
//long press
UILongPressGestureRecognizer *longPressGestureRecognizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressRecognizer:)];
longPressGestureRecognizer.numberOfTouchesRequired=1;
longPressGestureRecognizer.minimumPressDuration = 0.5f;
[longPressGestureRecognizer requireGestureRecognizerToFail:tapDouble];
longPressGestureRecognizer.delegate = self;
[self addGestureRecognizer:longPressGestureRecognizer];
这意味着长按手势等待单次点击。
您可以为长按手势添加时间。
UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(ontappLongPressGesture:)];
longPressGesture.minimumPressDuration=0.6;
longPressGesture.delegate=self;
[cell.view addGestureRecognizer:longPressGesture];
UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellSelected:)];
//[gesture requireGestureRecognizerToFail:longPressGesture];
gesture.delegate=self;
[cell.view addGestureRecognizer:gesture];
also you need to set this delegate to work both gesture together
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}