2015-11-29 39 views
5

我創建了一個SceneKit sceneUIViewController的遊戲。轉換UITapGestureRecognizer的SpriteKit的觸摸位置 - Swift

我實現了一個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位置?

謝謝!

回答

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

相關問題