我是这方面的新手,目前正在构建一个与AR相关的应用程序,在旧版本上我声明了这个
let results = self.hitTest(screenPosition, types: [.featurePoint])
现在我有一个问题,hitTest在iOS 14.0 中被弃用
hitTest(_:types:)' was deprecated in iOS 14.0: Use [ARSCNView raycastQueryFromPoint:allowingTarget:alignment]
请告诉我如何修复它,谢谢:(
是,使用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
}
...
您可以为节点分配一个名称,然后在touchesBegan
中使用hitTest(point:, options:[SCNHitTestOption : Any]?)
。下面是我使用的代码:
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")
}
}
}
}
别忘了在viewWillAppear中设置configuration.planeDetection在使用下面的代码之前,我假设你想在touchesBegan方法中使用
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
sceneView.session.run(configuration)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touchLocation = touches.first?.location(in: sceneView) else {return}
guard let query = sceneView.raycastQuery(from: touchLocation, allowing: .existingPlaneGeometry, alignment: .any) else {return}
let results = sceneView.session.raycast(query)
//this is the answer to your question, then you may want to get first result, then
if let hitResult = results.first{
//do what you want
}
}