1

I am trying to Update the globalDataTransfer class from the ARView extension and reflect the changes on to ArView. Below is the globalDataTransfer function

 class globalDataTransfer: ObservableObject{
    
    @Published var val: String = "No Value"
    
    required init  (any : String){
        self.val = any
    }
    
    func check() -> String{
        return(self.val)
    }
}

My ARViewContainer

struct ARViewContainer: UIViewRepresentable  {
    
    func makeUIView(context: UIViewRepresentableContext<ARViewContainer>) -> ARView {
        let arView = ARView(frame: .zero,cameraMode: .ar,automaticallyConfigureSession: true)
        arView.setupForBodyTracking()
        arView.scene.addAnchor(bodySkeletonAnchor)
        return(arView)
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
    }
    
    typealias UIViewType = ARView
}

My ARView extension is

extension ARView: ARSessionDelegate{
    
    func setupForBodyTracking(){
        let config = ARBodyTrackingConfiguration()
        self.session.run(config)
        self.session.delegate = self
    }
    
    public func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        
        for anchor in anchors{
            if let bodyAnchor = anchor as? ARBodyAnchor{
                if let skeleton = bodySkeleton{
                    skeleton.update(with: bodyAnchor)
                    var 
                }
                else{
                    let skeleton = BodySkeleton(for: bodyAnchor)
                    bodySkeleton = skeleton
                    bodySkeletonAnchor.addChild(skeleton)
                }
                var Val:String = calcAngle(anchor: bodyAnchor) //Function that returns Joint Angles and lengths
            }
        }
    }
}

Content View :



struct ArView: View {
    
    @ObservedObject var forGlobalValue: globalDataTransfer = globalDataTransfer(any: "No Value")

    var body: some View{
        ZStack{
            ARViewContainer()
            VStack{
                Text(forGlobalValue.val)
                Text("  This is \(test(any: forGlobalValue))")
            }
        }
    }
}

Can anyone let me know How do I pass this Observable object to the ARView or is there any way of getting the Val variable from ARView extension and update it regularly on my View Text.

  • Can you just pass the object into the ARViewContainer? What have you tried so far? – jtbandes Nov 03 '20 at 00:06
  • I tried to pass the object to the ARViewContainer which again calls the ARView Session didUpdate function. I am not sure how to pass it to the Session because I think session would be called internally from the ARView Container. – Kavin Amutha Nov 03 '20 at 03:38
  • Here are two possible approaches for similar scenarios: https://stackoverflow.com/a/64039359/12299030 and https://stackoverflow.com/a/58825642/12299030. – Asperi Nov 03 '20 at 03:48
  • I tried Implementing this solution https://stackoverflow.com/a/64039359/12299030 . But it throws me a "Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f6b7fe0)" – Kavin Amutha Nov 03 '20 at 17:53
  • Kavin, did you fix this? Did you look into using the Combine framework to pass values? – user3069232 Dec 17 '22 at 06:35

0 Answers0