为什么我们总是必须在命中测试中使用集合的第一个元素,而不是最后一个?

Why we always have to use a first element of the collection in hit-testing, not last?

为什么我们必须使用 hitTestResult.first 尽管我们可以在屏幕上点击几次并且每次点击都会作为 hitTestResult.last 写入数组?

@objc func tapped(gesture: UITapGestureRecognizer) {

    let touchPosition: CGPoint = gesture.location(in: sceneView)
    let hitTestResult: [ARHitTestResult] = sceneView.hitTest(touchPosition, types: .existingPlaneUsingExtent)

        if !hitTestResult.isEmpty {

            guard let hitResult = hitTestResult.first else {
                return
            }
            addGrass(hitTestResult: hitResult)
        }
    }
}

点击几次完全不会影响hitTest的结果。如果用户使用 1 个对象在同一个地方点击 100 次,您仍然只能从 hitTest 方法中获得 1 个对象。

根据找到的文档 here,将返回的对象是 "sorted from nearest to farthest (in distance from the camera)"。

因此,为了尝试更基本地理解事物,您可以在任何给定时间在屏幕上显示任意数量的对象。在任何时候,您都可以使用 hitTest,这将为您提供在您通过 touchPosition 定义的视图部分可见的所有对象。由于同一 view-location 中可能有多个项目,这些项目在数组中检索,该数组已排序,因此离您较近的项目位于数组的开头。

所以把first改成last就意味着宁愿用后面的对象而不用前面的对象。