TapGesture

A gesture that recognizes one or more taps.

StructureiOS 13.0+macOS 10.15+tvOS 16.0+watchOS 6.0+TapGesture in SwiftUI

Declaration

React
type TapGesture = { count?: number; } | { count?: number; inputKinds?: GestureInputKinds; };
Swift
nonisolated struct TapGesture

Type methods

Overview

To recognize a tap gesture on a view, create and configure the gesture, and then add it to the view using the View.gesture(_:including:) modifier. The following code adds a tap gesture to a Circle that toggles the color of the circle:

struct TapGestureView: View {
    @State private var tapped = false

    var tap: some Gesture {
        TapGesture(count: 1)
            .onEnded { _ in self.tapped = !self.tapped }
    }

    var body: some View {
        Circle()
            .fill(self.tapped ? Color.blue : Color.red)
            .frame(width: 100, height: 100, alignment: .center)
            .gesture(tap)
    }
}

From Apple’s SwiftUI documentation. Code samples there are Swift.

See also

More in Gestures and input.