同时使用委托,但仍然有一个手势覆盖其他手势
Posted
技术标签:
【中文标题】同时使用委托,但仍然有一个手势覆盖其他手势【英文标题】:Uses simultaneously delegate, but still one gesture overrides other 【发布时间】: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,然后将它们添加到每一个。以防万一我仍然订阅同时代表 但这仍然有点脏,所以如果您知道更好的变体,我将不胜感激
【问题讨论】:
【参考方案1】:据我所知,您正在将所有手势添加到self.view
。如果您改为将每个手势添加到它所适用的视图组件中,那么您甚至不必费心尝试确保同时性。
【讨论】:
所以我尝试了这个,现在它只是不像我之前提到的那样定期工作。甚至 tapDownGesture 根本不起作用以上是关于同时使用委托,但仍然有一个手势覆盖其他手势的主要内容,如果未能解决你的问题,请参考以下文章
UIPageViewController 手势覆盖我的 UITableView