我使用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.y
与myNode
y位置的y位置相反。
如何反转触摸的y位置?
谢谢!
答案 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)
}
}