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

时间:2015-11-29 17:55:31

标签: ios swift touch scenekit uitapgesturerecognizer

我使用SceneKit scene创建了一个UIViewController的游戏。

我实现了UITapGestureRecognizer这样:

// TAP GESTURE

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

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

        }

    }

}

想象一下myNode的位置是(x: 150.0, y: 150.0)。 问题是touchPosition.ymyNode y位置的y位置相反。

如何反转触摸的y位置?

谢谢!

1 个答案:

答案 0 :(得分: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)

    }   
}