swift - 确定 UITouch 是否在 SKLabelNode 框架内

标签 swift sprite-kit uitouch sklabelnode

我的目标是查明 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/

相关文章:

Swift 3 谷歌地图在触摸时添加标记

ios - SpriteKit 动画问题

xcode - SpriteKit Sound 导致崩溃

swift - 如何在 Swift 中将节点添加到不同类的 View 中

ios - iOS9.1 的UITouchType

ios - 如何检测 View 中的某些部分

swift - SWReveal TableViewController - 如何启动到不同的 ViewControllers?

arrays - 如何过滤包含字符串的数组和结构体对象数组,其中有字符串参数?

ios - 多点触控时 UITouches "lost"

swift - firebase .indexOn 警告与 autoId