为什么我不能在动作功能中切换UIGestureRecognizer的属性

时间:2017-07-05 05:25:43

标签: swift uigesturerecognizer

我有两个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)

handelDoublehandleSingle是处理双击和单击的函数。这非常有效。

现在我想创建一个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个实例吗?

1 个答案:

答案 0 :(得分:0)

在您的代码中,numberOfTapsRequired的{​​{1}}等于singleTap。我认为正确的值是2

1