我知道我可以使用requireGestureRecognizerToFail
功能区分单击和双击,但我遇到一个小问题,我想解决它。我的代码如下:
- (IBAction)singleTap:(UITapGestureRecognizer *)sender {
NSLOGD_METADATAONLY();
hideNavigationBar();
hideStatusBar();
[sender requireGestureRecognizerToFail:self.doubleTapRecognizer];
}
- (IBAction)doubleTap:(UITapGestureRecognizer *)sender {
NSLOGD_METADATAONLY();
//TODO
}
当我打开文件并双击(action1)时,将首先调用单击(result1),然后调用双击(result2)。
但是如果我打开一个文件并单击(action3),然后双击(action4),action4的结果运行良好,单击将不会被调用只有双击才会被调用。我想这是因为在action3中调用函数requireGestureRecognizerToFail
。
我的问题是如何让action1只调用result2而不是调用result1?
答案 0 :(得分:0)
在ViewDidLoad中写下以下行 [singleTap requireGestureRecognizerToFail:doubleTap];
答案 1 :(得分:0)
创建[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer]
UITapGestureRecognizer
更新代码
示例:强>
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
[view addGestureRecognizer: singleTapRecognizer];
// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[view addGestureRecognizer: doubleTapRecognizer];
当您为手势识别器设置requireGestureRecognizerToFail:
时,您说它应该只识别手势,如果其他手势识别器没有。
答案 2 :(得分:0)
将您的课程扩展为UIGestureRecognizerDelegate
。
class ViewController: UIViewController, UIGestureRecognizerDelegate {
然后将视图控制器指定为手势识别器的委托。
tapGesture .delegate = self;
doubleTapGesture .delegate = self;
shouldRequireFailureOf
的实现可防止在另一个手势识别器明确达到失败状态之前识别一个手势。
只有在双击失败后才能识别单击,当触摸序列仅包含一次点击时就会发生这种情况。 这可以通过以下方式实现。
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return false
}
希望它有所帮助。快乐编码!!