9

我是新手,目前正在构建一个与 AR 相关的应用程序,在旧版本上我说过这个

let results = self.hitTest(screenPosition, types: [.featurePoint])

现在我遇到了一个问题,即在 iOS 14.0 中不推荐使用 hitTest

hitTest(_:types:)' was deprecated in iOS 14.0: Use [ARSCNView raycastQueryFromPoint:allowingTarget:alignment]

请告诉我如何解决它,谢谢:)

4

2 回答 2

2

您可以为节点分配一个名称,然后使用hitTest(point:, options:[SCNHitTestOption : Any]?)in touchesBegan。下面是我使用的代码:

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, options: [SCNHitTestOption.searchMode : 1])
        
        for result in results.filter({$0.node.name != nil}) {
            if result.node.name == "planeNode" {
                print("touched the planeNode")
            }
        }
    }
}
于 2020-10-22T23:37:16.140 回答
2

是的,使用raycastQuery(from:allowing:alignment:)

正如Xcode以这种方式建议的那样:

...
let location = gesture.location(in: sceneView)
guard let query = sceneView.raycastQuery(from: location, allowing: .existingPlaneInfinite, alignment: .any) else {
   return
}
        
let results = sceneView.session.raycast(query)
guard let hitTestResult = results.first else {
   print("No surface found")
   return
}
...
于 2020-11-09T17:00:29.197 回答