【发布时间】:2020-04-11 20:06:39
【问题描述】:
所以我正在制作一个简单的 2D 游戏,我制作了 2 个用于移动的按钮和一个用于瞄准的操纵杆。因此,我制作了一个最短持续时间 = 0 的 longPressGesture 来检测它何时触动,以及两个用于按钮和操纵杆的平移手势。对于按钮,我选择 pan 是因为我想滑过按钮并让它们做一些事情,而不是一次又一次地按下。
这是我创建手势的地方。
func createGestures() {
var gestureArray = [UIGestureRecognizer]()
//tapDownGesture
let tapDownGesture = UILongPressGestureRecognizer(target: self, action: #selector(tapDownFunc(sender:)))
tapDownGesture.minimumPressDuration = 0
tapDownGesture.name = "longGesture"
//tapUpGesture
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapFunc(sender:)))
//panGesture
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panFunc(sender:)))
let secondPanGesture = UIPanGestureRecognizer(target: self, action: #selector(panFunc(sender:)))
secondPanGesture.name = "panGesture"
panGesture.name = "panGesture"
tapDownGesture.delegate = self
secondPanGesture.delegate = self
panGesture.delegate = self
tapGesture.delegate = self
//gestureArray.append(tapGesture)
gestureArray.append(secondPanGesture)
gestureArray.append(panGesture)
gestureArray.append(tapDownGesture)
for i in gestureArray {
self.view.addGestureRecognizer(i)
}
}
还有委托
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
我一直在寻找解决方案,但我发现我需要使用我已经在使用的同步委托。
因此,使用该代码我可以使用操纵杆,但仅在未按下按钮时。当按下按钮时,我无法开始使用 panGesture,或者如果它已经在使用它,它会在按钮释放后停止并继续。
此外,没有代码会失败其他手势或类似的东西。 按钮是 UIButton,摇杆是 SKNode
编辑: 所以,我通过为操纵杆制作一个 uiview 来解决这个问题(因为 sknode 类不能有手势识别器),然后为每个按钮和操纵杆制作一个不同的 GestureRecognizer,然后将它们添加到每一个。以防万一我仍然订阅同时代表 但这仍然有点脏,所以如果您知道更好的变体,我将不胜感激
【问题讨论】:
标签: ios swift sprite-kit uigesturerecognizer