Multiple taps with TapGesture

We can specify a count to indicate the number of taps required to trigger a TapGesture.

This lets us create simple double or triple tap options in our apps.

struct Example026: View {

    @State var selected: Entity? = nil

    var body: some View {
        RealityView { content in
            if let scene = try? await Entity(named: "GestureLabs", in: realityKitContentBundle) {
                content.add(scene)
                scene.position.y = -0.4
            }
        }
        .gesture(tapExample)
    }

    var tapExample: some Gesture {
        // Set the count to the number of taps to trigger the gesture
        TapGesture(count: 2)
            .targetedToAnyEntity()
            .onEnded { value in
                if selected === value.entity {
                    selected?.position.y = 0
                    selected = nil
                } else {
                    selected?.position.y = 0
                    value.entity.position.y = 0.1
                    selected = value.entity
                }
            }
    }
}

For more information on using this gesture, see: TapGesture.

Support our work so we can continue to bring you new examples and articles.

Questions or feedback?