Search code examples
swiftsprite-kituitouchsklabelnode

Determine if UITouch is within SKLabelNode frame


My goal is to find out if a UITouch took place within a SpriteKit label node. The view is an SKView. The issue with the code that I am using (below) is that the touch and the rectangle seem to be in different coordinate systems.

I could use simple math to correct this, but is there a simple way to correct this issue? Or is there another way I should be doing this?

 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")
        }
    }
 }

Solution

  • I assume that you are are overriding touchesBegan within a scene. If so, then try using let position = t.location(in: self), i.e. replace view with self. That way you get the position within the scene itself, rather than the position within the view that holds the scene.

    Hope that helps!