【问题标题】:How can I access the indices of a hit test?如何访问命中测试的索引?
【发布时间】:2019-11-22 00:58:59
【问题描述】:

我正在尝试通过命中测试将不同的对象添加到场景中,而我目前只能添加一个对象。我想访问命中测试结果中的第二个、第三个和第四个索引,以便通过触摸添加这些对象。

我尝试通过.indices访问,但它与UITouch不兼容。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if let touch = touches.first {

        let touchLocation = touch.location(in: sceneView)

        let results = sceneView.hitTest(touchLocation, types: .featurePoint)

        if let hitResult = results.first {
            let cubeScene = SCNScene(named: "art.scnassets/cube.scn")!

            if let cubeNode = cubeScene.rootNode.childNode(withName: "cube", recursively: true) {


                sceneView.scene.rootNode.addChildNode(cubeNode)

            }
        }
    }
}

我应该怎么做?我考虑过创建一个 SCNScene,将其作为数组传递,然后从场景中加载所有模型。

【问题讨论】:

  • 也许你可以为结果数组做一个 for 循环,而不是仅仅检查第一个元素是否包含某些东西

标签: swift scenekit arkit


【解决方案1】:

正如 Samual 所建议的......示例

var hitTestOptions = [SCNHitTestOption: Any]()

    @objc func handleTap(recognizer: UITapGestureRecognizer)
        {
             let location: CGPoint = recognizer.location(in: gameScene)
             let hitResults: [SCNHitTestResult] = gameScene.hitTest(location, options: hitTestOptions)
             for vHit in hitResults
             {
              if(vHit.node.name?.prefix(5) == "Panel")
              {
               gameControl.selectPanel(vPanel: vHit.node.name!)
               return
              }
             }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 2016-11-07
    • 1970-01-01
    • 2012-11-05
    • 2011-07-05
    • 1970-01-01
    • 2019-06-12
    • 2016-08-03
    相关资源
    最近更新 更多