DragGesture for RealityView

I used such a gesture under a reality view.

DragGesture().targetedToAnyEntity()
            .onChanged { value in
                print("DragGesture")
                self.dragOffset = value.translation
                self.startTimer()
            }
            .onEnded { _ in
                self.dragOffset = .zero
                self.direction = "None"
                self.stopTimer()
            }

However, due to the special nature of Reality View, it is impossible to detect gestures normally, so I think some modifiers should be added after value.translation, but I don't know what modifiers are. Can you give me some? Do you know? Thank you.

Answered by Vision Pro Engineer in 798068022

Hey @lijiaxu,

If you haven't already, I'd highly recommend you take a look at the Respond to interactions with RealityKit content section of documentation and the Transforming RealityKit entities using gestures sample code. These provide some great code samples of using gestures.

I'm not sure issue you are encountering, are you successfully printing out "DragGesture"? Or is the onChanged block being called, but dragOffset is having no effect?

Have you added an InputTargetComponent and collision shapes to the RealityKit entities you'd like to have gestures on?

Another thing to note is that the gesture(_:including:) or simultaneousGesture(_:including:) view modifier should be applied directly on the RealityView.

Let me know if this helps,

Michael

Accepted Answer

Hey @lijiaxu,

If you haven't already, I'd highly recommend you take a look at the Respond to interactions with RealityKit content section of documentation and the Transforming RealityKit entities using gestures sample code. These provide some great code samples of using gestures.

I'm not sure issue you are encountering, are you successfully printing out "DragGesture"? Or is the onChanged block being called, but dragOffset is having no effect?

Have you added an InputTargetComponent and collision shapes to the RealityKit entities you'd like to have gestures on?

Another thing to note is that the gesture(_:including:) or simultaneousGesture(_:including:) view modifier should be applied directly on the RealityView.

Let me know if this helps,

Michael

DragGesture for RealityView
 
 
Q