我创建了一个带有SceneKit场景的游戏到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.y与myNode的y位置的y位置相反.
如何反转触摸的y位置?
谢谢 !
解决方法
试试这个,它对我来说很准确: –
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)
}
}