我的目标是查明 UITouch 是否发生在 SpriteKit 标签节点内。该 View 是一个 SKView。我使用的代码(如下)的问题是触摸和矩形似乎在不同的坐标系中。
我可以使用简单的数学来纠正这个问题,但是有没有一种简单的方法可以纠正这个问题?还是我应该这样做的另一种方式?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let position = t.location(in: view)
//if inside startButton
if (startButton?.frame.contains(position))! {
debugPrint("yes")
}
}
}
最佳答案
我假设您正在覆盖场景中的 touchesBegan
。如果是这样,则尝试使用 let position = t.location(in: self)
,即将 view
替换为 self
。这样您就可以获得场景本身内的位置,而不是持有场景的 View 内的位置。
希望对您有所帮助!
关于swift - 确定 UITouch 是否在 SKLabelNode 框架内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44036776/