How do I add a gesture to an entity?
Asked on 07/31/2024
1 search
To add a gesture to an entity in visionOS, you can follow these steps:
-
Ensure the Entity is Tappable: Any entities you want to be tappable need to have an input target component and a collision component.
-
Attach a Gesture to the Reality View: Attach the desired gesture to the reality view that contains your entities. This will make any entity within that view tappable.
-
Respond to the Gesture: Once the gesture is detected, you can respond to it in the gesture's handlers.
Here's a code snippet to illustrate this process:
// Ensure the entity has an input target and collision component
entity.components[InputTargetComponent.self] = InputTargetComponent()
entity.components[CollisionComponent.self] = CollisionComponent()
// Attach a tap gesture to the reality view
let tapGesture = TapGesture()
realityView.addGestureRecognizer(tapGesture)
// Handle the tap gesture
tapGesture.addTarget(self, action: #selector(handleTap(_:)))
@objc func handleTap(_ gesture: TapGesture) {
if let entity = gesture.entity {
// Respond to the tap gesture on the entity
// For example, trigger an animation or change the entity's state
}
}
For more detailed information, you can refer to the session Explore game input in visionOS.
Relevant Sessions

Compose interactive 3D content in Reality Composer Pro
Discover how the Timeline view in Reality Composer Pro can bring your 3D content to life. Learn how to create an animated story in which characters and objects interact with each other and the world around them using inverse kinematics, blend shapes, and skeletal poses. We’ll also show you how to use built-in and custom actions, sequence your actions, apply triggers, and implement natural movements.

What’s new in UIKit
Explore everything new in UIKit, including tab and document launch experiences, transitions, and text and input changes. We’ll also discuss better-than-ever interoperability between UIKit and SwiftUI animations and gestures, as well as general improvements throughout UIKit.

Meet TabletopKit for visionOS
Build a board game for visionOS from scratch using TabletopKit. We’ll show you how to set up your game, add powerful rendering using RealityKit, and enable multiplayer using spatial Personas in FaceTime with only a few extra lines of code.