2015-11-29 12 views
5

Ho creato un gioco con un SceneKit scene in un UIViewController.Converti touch location di UITapGestureRecognizer per SpriteKit - Swift

ho implementato un UITapGestureRecognizer come questo:

// TAP GESTURE 

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

E la funzione 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 

     } 

    } 

} 

Immaginate che la posizione myNode s' è (x: 150.0, y: 150.0). Il problema è che lo touchPosition.y si trova al contrario della posizione y della posizione myNode.

Come si inverte la posizione del tocco?

Grazie!

risposta

7

Prova a modificare, la sua accurata per 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) 

    } 
} 
+0

Dove è l'attuazione di convertPointFromView? –

+0

questo metodo override didMovetoView non è chiamato nella mia classe UIviewcontroller. perché? – Hitesh

Problemi correlati