2015-11-29 8 views
5

SceneKit sceneのゲームをUIViewControllerに作成しました。SpriteKit - SwiftのUITapGestureRecognizerのタッチ位置を変換する

私はこのような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)であることを想像してみてください。 問題はmyNodeのy位置の反対側にtouchPosition.yがあることです。

タッチの位置をどのように反転できますか?

ありがとうございます!

答えて

7

私のためにその正確、代わりにこれを試してください: -

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) 

    } 
} 
+0

どこconvertPointFromViewの実装はありますか? –

+0

このオーバーライドdidMovetoViewメソッドはMy UIviewcontrollerクラスで呼び出されません。どうして? – Hitesh

関連する問題