Structure

TapGesture

A gesture that recognizes one or more taps.

Declaration

struct TapGesture

Overview

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

struct TapGestureView: View {
    @State 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)
    }
}

Topics

Creating a Tap Gesture

init(count: Int)

Creates a tap gesture with the number of required taps.

var count: Int

The required number of tap events.

typealias TapGesture.Body

A type that represents the tap gesture and its behavior.

Performing the Gesture

func onEnded((()) -> Void) -> _EndedGesture<TapGesture>

Adds an action to perform when the gesture ends.

typealias TapGesture.Value

A type that describes the value of a tap gesture.

Composing Gestures

func simultaneously<Other>(with: Other) -> SimultaneousGesture<TapGesture, Other>

Combines a gesture with another gesture to create a new gesture that recognizes both gestures at the same time.

func sequenced<Other>(before: Other) -> SequenceGesture<TapGesture, Other>

Sequences a gesture with another one to create a new gesture, which results in the second gesture only receiving events after the first gesture succeeds.

func exclusively<Other>(before: Other) -> ExclusiveGesture<TapGesture, Other>

Combines two gestures exclusively to create a new gesture where only one gesture succeeds, giving precedence to the first gesture.

Adding Modifier Keys to a Gesture

func modifiers(EventModifiers) -> _ModifiersGesture<TapGesture>

Combines a gesture with keyboard modifiers.

Transforming a Gesture

func map<T>((()) -> T) -> _MapGesture<TapGesture, T>

Returns a gesture that’s the result of mapping the given closure over the gesture.

Relationships

Conforms To

See Also

Basic Gestures

Adding Interactivity with Gestures

Use gesture modifiers to add interactivity to your app.

struct LongPressGesture

A gesture that succeeds when the user performs a long press.

struct DragGesture

A dragging motion that invokes an action as the drag-event sequence changes.

struct MagnificationGesture

A gesture that recognizes a magnification motion and tracks the amount of magnification.

struct RotationGesture

A gesture that recognizes a rotation motion and tracks the angle of the rotation.