在ViewController中检测SKNode的触摸

时间:2019-02-13 19:30:15

标签: ios swift sprite-kit

我有一个SKNode,一种操纵杆,插入到ViewController中显示的SKView中,如下所示:

touchesMoved()

如何使用功能 - macOS 10.12.16 - python 2.7.11 - opencv 4.0.0 - ipython 5.8.0 - jupyter notebook server 5.7.4 仅检测操纵杆上的触摸?如果我重写此方法,它将在所有视图中检测到触摸。 我只找到了答案,该类是SKScene的子类,而不是像我的UIViewController的子类。

2 个答案:

答案 0 :(得分:0)

通过使用touchesMoved(),您可以像这样检测节点的触摸:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let node : SKNode = self.atPoint(location)

        if node.name == "joystick" {
            // Something, something, something...
        }
    }
}

通过此操作,您需要首先设置节点的名称。

答案 1 :(得分:0)

class FeedVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    let joystick = Joystick()

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = MyScene(size: joystickView.frame.size)
        scene.backgroundColor = SKColor.white
        scene.addChild(joystick)
        joystick.position = CGPoint(x: scene.size.width / 2, y: scene.size.height / 2)
        joystickView.presentScene(scene)
    }
}

您的自定义SKScene类

class MyScene:SKScene {

    //... 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        //This touchesMoved is NOT equal to the touchesMoved used for the ViewController since this ViewController is for ONLY the scene.
    }
}