0

I am creating an ARView using UIViewRepresentable in SwiftUI, and I am trying to apply all EntityGestures to the model, but I am not sure why the gestures are not working and the ARView is not receiving any gestures. Here is the code:

    func makeUIView(context: Context) -> ARView {
    
        let view = ARView()
        .
        .
        .
        .
        
        // Handle ARSession events via delegate
        context.coordinator.view = view
        session.delegate = context.coordinator
        
        view.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(ARCoordinator.handleTap)))
        
        return view
    }

    func updateUIView(_ view: ARView, context: Context) { }
    
    func makeCoordinator() -> ARCoordinator {
        ARCoordinator()
    }
}


class ARCoordinator: NSObject, ARSessionDelegate {
    
    weak var view: ARView?
    var focusEntity: FocusEntity?
    private var isModelPlaced = false
    
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        guard let view = self.view else { return }
        debugPrint("Anchors added to the scene: ", anchors)
        self.focusEntity = FocusEntity(on: view, style: .classic(color: .yellow))
    }
    
    @objc func handleTap() {
    
        if isModelPlaced == false {
            guard let view = self.view, let focusEntity = self.focusEntity else { return }
            // Create a new anchor to add content to
            let anchor = AnchorEntity()
            view.scene.anchors.append(anchor)
            
            // Add a model
            let modelEntity = try! ModelEntity.loadModel(named: "Models.scnassets/ball")
            modelEntity.generateCollisionShapes(recursive: true)
            modelEntity.position = focusEntity.position
        
            view.installGestures(.all, for: modelEntity) //*** gestures is not working ***///
            
            focusEntity.hide()
            isModelPlaced = true
            anchor.addChild(modelEntity)
        }
    }
}

any help would be great

Mc.Lover
  • 4,813
  • 9
  • 46
  • 80
  • It is not clear where is the problem and code is not testable. Is your `handleTap` called on tap? – Asperi Jul 05 '22 at 04:06
  • Yes, It taps and adds the model, here is the file: https://www.dropbox.com/s/mmbe6yw6v2mhjd4/RealityKitView.swift?dl=0 – Mc.Lover Jul 05 '22 at 07:22

1 Answers1

0

As far as I'm aware, adding gestures to imported 3D models does not work the same as for simple generated shapes. Your code would work fine if you had a ModelEntity with a mesh of GenerateBox or GenerateSphere. I'm not sure why the process is different for imported models, though.

Here's a link to another question that might help: Enabling gestures in RealityKit

Someone on the apple developer forum had the same problem. If you can't find what you're looking for in the linked question above, this is your next best bet: https://developer.apple.com/forums/thread/119773

Am I correct that your code is from Mohammad Azam's Udemy course on RealityKit? If neither of those links work, you might have luck asking him directly in the Q&A.

Arcee
  • 204
  • 1
  • 8