Contents

SpatialTapGesture

A gesture that recognizes one or more taps and reports their location.

Declaration

struct SpatialTapGesture

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 based on the tap location:

struct TapGestureView: View {
    @State private var location: CGPoint = .zero

    var tap: some Gesture {
        SpatialTapGesture()
            .onEnded { event in
                self.location = event.location
             }
    }

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

Topics

Creating a spatial tap gesture

Getting the gesture’s value

Deprecated initializers

Initializers

See Also

Recognizing tap gestures