I have a class that contains an ARSCNView
:
class ARSessionCameraView: NSObject {
var delegate: ARViewWrapperDelegate?
var arKitView: ARSCNView = ARSCNView()
// Sets the frame of the ARKit view
func setupViews(arFrame: CGRect) {
arKitView = ARSCNView(frame: arFrame)
self.delegate?.viewsDidCreate(arKitView: arKitView)
}
// Sets the session delegate
func setupARSession(position: AVCaptureDevice.Position) {
if position == .back {
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal]
configuration.userFaceTrackingEnabled = true
self.arKitView.session.run(configuration)
} else if position == .front {
let configuration = ARFaceTrackingConfiguration()
configuration.isWorldTrackingEnabled = true
self.arKitView.session.run(configuration)
}
self.arKitView.session.delegate = self
...
}
I'm able to receive delegate update calls from ARSessionDelegate
:
extension ARSessionCameraView: ARSessionDelegate {
// Called whenever there is a new ARFrame from the ARSession
func session(_ session: ARSession, didUpdate frame: ARFrame) {
}
func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera) {
print(camera.trackingState)
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay.
}
}
But, for some reason can't receive delegate updates from these ARSCNViewDelegate
methods:
extension ARSessionCameraView: ARSCNViewDelegate {
/// - Tag: PlaceARContent
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
// Place content only for anchors found by plane detection.
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
}
/// - Tag: UpdateARContent
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
// Update only anchors and nodes set up by `renderer(_:didAdd:for:)`.
guard let planeAnchor = anchor as? ARPlaneAnchor
else { return }
}
}
CodePudding user response:
You never set the delegate
property on your ARSCNView
instance.
func setupViews(arFrame: CGRect) {
arKitView = ARSCNView(frame: arFrame)
arKitView.delegate = self // Add this line
self.delegate?.viewsDidCreate(arKitView: arKitView)
}
And you most likely also need to add arKitView
to a parent view in order for those delegate methods to be called.