我们的项目有自定义UICollectionViewCell,可以使用UITapGestureRecognizers处理单击和双击,以及使用单击UITapGestureRecognizer的子视图。
这就是我们项目中单击/双击设置的方式:
_singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
_singleTapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:_singleTapGestureRecognizer];
_doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
_doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:_doubleTapGestureRecognizer]
[_singleTapGestureRecognizer requireGestureRecognizerToFail:_doubleTapGestureRecognizer];
在我们的代码中,我们还通过执行这些来取消单/双击,
self.singleTapGestureRecognizer.enabled = NO;
self.doubleTapGestureRecognizer.enabled = NO;
self.doubleTapGestureRecognizer.enabled = YES;
self.singleTapGestureRecognizer.enabled = YES;
这些曾经在iOS9之前的设备上运行得很好,但是,我发现在iOS9设备上,从不触发双击。每当我试图双击该单元时,它立即记录一次,几乎没有延迟。
当我试图查看它时,我发现当注册singleTapGestureRecognizer时,doubleTapGestureRecognizer仍处于可能未失败的状态。
single tap: <UITapGestureRecognizer: 0x7f94ead42220; state = Ended; view = <UIView 0x7f94ead3f710>; target= <(action=handleSingleTap:, target=<PVEntityStreamCellForSetOnboarding 0x7f94ead3f1e0>)>; must-fail = {
<UITapGestureRecognizer: 0x7f94ead42810; state = Possible; view = <UIView 0x7f94ead3f710>; target= <(action=handleDoubleTap:, target=<PVEntityStreamCellForSetOnboarding 0x7f94ead3f1e0>)>; numberOfTapsRequired = 2>
}>
我还发现如果我首先点击包含另一个UITapGestureRecognizer的子视图,那么双击将起作用,一旦我滚动集合视图,就不可能双击任何单元格。
有没有人有类似的问题或知道如何解决这个问题?
答案 0 :(得分:0)
我有一个类似的问题,在我的情况下,修复程序是为singleTapGestureRecognizer实现委托方法。我也尝试使用取消单/双击,但没有成功。现在,我的代码看起来像这样(它是用swift编写的,但它很容易阅读):
let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleSingleTap:"))
let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleDoubleTap:"))
singleTapGestureRecognizer.numberOfTapsRequired = 1
doubleTapGestureRecognizer.numberOfTapsRequired = 2
singleTapGestureRecognizer.delegate = self
singleTapGestureRecognizer.requireGestureRecognizerToFail(doubleTapGestureRecognizer)
view.addGestureRecognizer(singleTapGestureRecognizer)
view.addGestureRecognizer(doubleTapGestureRecognizer)