Convert Touch Location UITapGestureRecognizer for SpriteKit - Swift

I created a game with a SceneKit scene in a UIViewController .

I implemented a UITapGestureRecognizer as follows:

 // TAP GESTURE let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:") sceneView.overlaySKScene?.view!.addGestureRecognizer(tapGesture) 

And the handleTap function:

 func handleTap(sender: UIGestureRecognizer) { if sender.state == UIGestureRecognizerState.Ended { var touchLocation = sender.locationInView(sender.view) touchLocation = self.view.convertPoint(touchLocation, toView: self.view) let touchedNode = sceneView.overlaySKScene?.nodeAtPoint(touchLocation) if touchedNode == myNode { // DO SOMETHING } } } 

Suppose the position is myNode (x: 150.0, y: 150.0) . The problem is that touchPosition.y is in the opposite y position of myNode y.

How can i invert y by touch?

Thanks!

+5
source share
1 answer

Try this instead, its exact for me: -

 override func didMoveToView(view: SKView) { let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tap:")) tap.numberOfTapsRequired = 1 view.addGestureRecognizer(tap) } func tap(sender:UITapGestureRecognizer){ if sender.state == .Ended { var touchLocation: CGPoint = sender.locationInView(sender.view) touchLocation = self.convertPointFromView(touchLocation) } } 
+7
source

Source: https://habr.com/ru/post/1237061/


All Articles