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