为 SpriteKit 转换 UITapGestureRecognizer 的触摸位置 - Swift
Convert touch location of UITapGestureRecognizer for SpriteKit - Swift
我创建了一个游戏 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 位置?
谢谢!
试试这个,它对我来说是准确的:-
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)
}
}
我创建了一个游戏 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 位置?
谢谢!
试试这个,它对我来说是准确的:-
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)
}
}