我有两个UITapGestureRecognizer
来处理单击和双击。我的代码是这样的:
let doubleTap = UITapGestureRecognizer(
target: self, action: #selector(viewController.handleDouble)
)
doubleTap.numberOfTapsRequired = 2
myview.addGestureRecognizer(doubleTap)
let singleTap = UITapGestureRecognizer(
target: self, action: #selector(viewController.handleSingle)
)
singleTap.numberOfTapsRequired = 1
singleTap.require(toFail: doubleTap)
myview.addGestureRecognizer(singleTap)
handelDouble
和handleSingle
是处理双击和单击的函数。这非常有效。
现在我想创建一个UITapGestureRecognizer
来处理这两种情况,例如:
let tap = UITapGestureRecognizer(
target: self, action: #selector(viewController.handleTap(recognizer:))
)
myview.addGestureRecognizer(Tap)
在我写的handelTap
函数中:
func handleTap(recognizer: UITapGestureRecognizer) {
switch recognizer.state {
case .ended:
if (recognizer.numberOfTapsRequired = 2) {
// to do something with double tapping
} else {
// to do something with single tapping
}
default: break
}
}
然而,这不起作用。更奇怪的是,当双击时,发生单次攻击的事情。
这也发生在UISwipeGestureRecognizer
,因为我无法使用一个函数来选择它的方向。
那么我们只能为这样的工作创建多个UIGestureRecognizer
个实例吗?
答案 0 :(得分:0)
在您的代码中,numberOfTapsRequired
的{{1}}等于singleTap
。我认为正确的值是2
1