为SpriteKit转换UITapGestureRecognizer的触摸位置 - Swift

fre*_*dnd 8 touch ios uitapgesturerecognizer scenekit swift

我创建了一个游戏SceneKit sceneUIViewController.

我实现了UITapGestureRecognizer这样的:

// TAP GESTURE

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
sceneView.overlaySKScene?.view!.addGestureRecognizer(tapGesture)
Run Code Online (Sandbox Code Playgroud)

handleTap功能:

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

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

想象一下,myNode这个位置是(x: 150.0, y: 150.0).问题在于它与'sy位置' touchPosition.y的y位置相反myNode.

如何反转触摸的y位置?

谢谢 !

Rac*_*hel 11

试试这个,它对我来说很准确: -

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)

    }   
}
Run Code Online (Sandbox Code Playgroud)