我目前正在开发使用UIGestureRecognizers的游戏。我正在使用平移手势来移动播放器,点击手势来检测其他UI按钮的触摸。除了这两个手势之间存在冲突之外,其他所有操作似乎都正常。每当玩家移动时(识别出平移手势),游戏都会忽略我所有的轻击手势(一旦识别了平移手势,视图将不会识别出轻击手势)。 有人可以告诉我如何使这两个手势一起工作吗?我希望播放器在UI按钮被点击时停止移动。换句话说,我希望在识别到轻击手势时取消平移手势。
非常感谢您!
这是我设置两个手势的方式:
let singleTap = UITapGestureRecognizer(target: self, action: #selector(doSingleTap))
singleTap.numberOfTapsRequired = 1
singleTap.delegate = self
self.view?.addGestureRecognizer(singleTap)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
panGesture.minimumNumberOfTouches = 1
panGesture.delegate = self
self.view?.addGestureRecognizer(panGesture)
@objc func handlePan(gestureReconizer: UIPanGestureRecognizer) {
if isPaused || player.isInAction {return}
let translation = gestureReconizer.translation(in: self.view)
if gestureReconizer.state == .changed {
let angle = 180 + (atan2(-translation.x, translation.y) * (180/3.14159))
player.movementAngle = angle
player.atState = .Walk
}
if gestureReconizer.state == .ended {
player.movementAngle = 0.0
if player.atState == .Walk {
player.atState = .Idle
}
}
}
@objc func doSingleTap(gestureReconizer: UITapGestureRecognizer) {
let originaTapLocation = gestureReconizer.location(in: self.view)
let location = convertPoint(fromView: originaTapLocation)
let node = atPoint(location)
switch node.name {
case "HeroAvatar":
//do stuff here
break
case "Fire":
//do stuff here
break
case "Potion":
//do stuff here
break
default:
break
}
}
答案 0 :(得分:1)
您需要实现UIGestureRecognizerDelegate
的委托方法,如下所示:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == singleTap &&
otherGestureRecognizer == panGesture {
return true
}
return false
}
希望这对您有用:)