Is there a way to get CGPoint of tap location in SwiftUI using RealityKit?

I am developing an app in SwiftUI where i can place an object on detected plane on screen tap. However i need to give screen location to implement

arView.raycast(from: CGPoint, allowing: ARRaycastQuery.Target, alignment: ARRaycastQuery.TargetAlignment)

In UIKit i can easily get location with the handleTap function using the method below

arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:))))

@objc func handleTap(recognizer: UITapGestureRecognizer){     let tapLocation = recognizer.location(in: arView) }

How can i implement this in swiftUI structs to place objects on plane where tapped?

Accepted Reply

Hello,

My recommendation is that you implement a UIViewControllerRepresentable that would wrap a UIViewController where you have set up the tap gesture.

Replies

Hello,

My recommendation is that you implement a UIViewControllerRepresentable that would wrap a UIViewController where you have set up the tap gesture.