代码之家  ›  专栏  ›  技术社区  ›  diegomen

在一个平面上放置一个SCNNode并检测该节点是否被点击

  •  0
  • diegomen  · 技术社区  · 7 年前

        @objc func tapped(sender: UITapGestureRecognizer) {
            let sceneView = sender.view as! ARSCNView
            let tapLocation = sender.location(in: sceneView)
            let hitTest = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
            if !hitTest.isEmpty {
                addItem(hitTestResult: hitTest.first!)
                hideTip()
        }
    }
    

    它是在点击平面时添加节点的,但是现在我想检测节点何时被点击,我使用了以下代码:

        let sceneView = sender.view as! ARSCNView
        let tapLocation = sender.location(in: sceneView)
        let hitTouchTest = sceneView.hitTest(tapLocation)
        if !hitTouchTest.isEmpty {
            let results = hitTouchTest.first!
            let node = results.node
        }
    

    它输入if,但节点的名称总是nil,当我创建节点并将其添加到平面时,我给它一个名称。。。如何检测节点已被点击?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Yevhen Ibrahim    7 年前

    此解决方案的问题是,需要获取已接触的节点数组的第一个值。 我建议阅读下一个主题: https://developer.apple.com/documentation/scenekit/scnhittestoption

    func registerGestureRecognizer() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(search))
        self.sceneView.addGestureRecognizer(tap)
    }
    
    @objc func search(sender: UITapGestureRecognizer) {
        let sceneView = sender.view as! ARSCNView
        let location = sender.location(in: sceneView)
        let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1])
    
        guard sender.state == .began else { return }
            for result in results.filter( { $0.node.name != nil }) {
                if result.node.name == "Your node name" {
                    // do manipulations
                }
            }
    }
    

    另外,这种方法可以帮助您通过其名称获取特定节点。

    推荐文章