ios – 为SpriteKit转换UITapGestureRecognizer的触摸位置 – Swift

前端之家收集整理的这篇文章主要介绍了ios – 为SpriteKit转换UITapGestureRecognizer的触摸位置 – Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个带有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)

    }   
}
原文链接:/iOS/328806.html

猜你在找的iOS相关文章