'hitTest()' was Depecrated in iOS 14.0
Asked Answered
T

3

11

I'm new to this and currently building an AR-related application, on the old version I stated this

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

and now I have a problem where the hitTest is deprecated in iOS 14.0

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

please advise me on how to fix it, thank you :)

Thinner answered 8/10, 2020 at 7:51 Comment(1)
you should use raycastQuery(from:allowing:alignment:) to do this. could you please check this to understand what is it and how can you use it -> https://mcmap.net/q/1019516/-in-which-cases-arscnview-raycastquery-returns-nil/7512091Counter
C
6

Yes, use raycastQuery(from:allowing:alignment:)

as suggested by Xcode in this way:

...
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
}
...
Catamount answered 9/11, 2020 at 17:0 Comment(0)
E
2

You can assign a node with a name and then use hitTest(point:, options:[SCNHitTestOption : Any]?) in touchesBegan. Below is the code I used:

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")
            }
        }
    }
}
Eccrinology answered 22/10, 2020 at 23:37 Comment(0)
M
0

Don't forget to set configuration.planeDetection in viewWillAppear before using the code below, I assume that you want to use in touchesBegan method

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
                
    }
}
Methanol answered 12/5, 2022 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.