gpt4 book ai didi

swift - 为什么我无法在操作函数中切换 UIGestureRecognizer 的属性

转载 作者:行者123 更新时间:2023-11-30 12:19:56 27 4
gpt4 key购买 nike

我有两个 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 实例来完成这样的工作?

最佳答案

在您的代码中,singleTapnumberOfTapsRequired 等于 2。我认为正确的值是 1

let singleTap = UITapGestureRecognizer(
target: self, action: #selector(viewController.handleSingle)
)
singleTap.numberOfTapsRequired = 2
singleTap.require(toFail: doubleTap)
myview.addGestureRecognizer(singleTap)

关于swift - 为什么我无法在操作函数中切换 UIGestureRecognizer 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44917612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com