Long Press Gesture
We can use Long Press Gesture to perform an action or update our scene after a delay.
In this example, we use the .onEnded closure to start a move(to...) animation on an entity. When we tap and hold for 1 second, the entity is raised and scaled up. We can tap to lower it back to its origin.
struct Example006: View {
@State var selected: Entity? = nil
var body: some View {
RealityView { content in
// Load the scene from the Reality Kit bundle
if let scene = try? await Entity(named: "GestureLabs", in: realityKitContentBundle) {
content.add(scene)
// Lower the entire scene to the bottom of the volume
scene.position.y = -0.4
}
}
.gesture(tapExample)
.gesture(longPress)
}
var longPress: some Gesture {
LongPressGesture(minimumDuration: 1)
.targetedToAnyEntity()
.onEnded { value in
if selected === value.entity {
lowerEntity(value.entity)
} else {
// Lower an existing selected entity
if let selected {
lowerEntity(selected)
}
// Raise the long-pressed entity
raiseEntity(value.entity)
selected = value.entity
}
}
}
var tapExample: some Gesture {
TapGesture()
.targetedToAnyEntity()
.onEnded { value in
lowerEntity(value.entity)
}
}
func raiseEntity(_ entity: Entity) {
...
}
func lowerEntity(_ entity: Entity) {
...
}
}One thing to keep in mind. When using both tap gesture and long press gesture, make sure to place the tap gesture first. If I reversed the order of these then the tapExample would never fire.
.gesture(tapExample)
.gesture(longPress)I don’t often use this gesture in my designs, but I have found it useful when combined with other gestures. For example, a sequential gesture that uses long press to delay the start of a drag gesture. I suppose it could also be useful for showing and hiding an attachment on an entity, sort of like a context menu in 3D.
Long Press Gesture also has an updating closure, but I haven’t found much use for it in RealityKit. It seems more suited for animating SwiftUI state that can be used directly in modifiers, but we don’t update our Entities like that. If you know of a good example of using the updating closure please let me know I will update this page.
Video Demo
Support our work so we can continue to bring you new examples and articles.
Download the Xcode project with this and many more examples from Step Into Vision.
Some examples are provided as standalone Xcode projects. You can find those here.

Follow Step Into Vision