DragGesture

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

StructureiOS 13.0+macOS 10.15+watchOS 6.0+DragGesture in SwiftUI

Declaration

React
type DragGesture = { minimumDistance?: number; coordinateSpace?: CoordinateSpaceProtocol; } | { minimumDistance?: number; coordinateSpace?: CoordinateSpaceProtocol; inputKinds?: GestureInputKinds; };
Swift
@MainActor @preconcurrency struct DragGesture

Overview

To recognize a drag gesture on a view, create and configure the gesture, and then add it to the view using the View.gesture(_:including:) modifier.

Add a drag gesture to a Circle and change its color while the user performs the drag gesture:

struct DragGestureView: View {
    @State private var isDragging = false

    var drag: some Gesture {
        DragGesture()
            .onChanged { _ in self.isDragging = true }
            .onEnded { _ in self.isDragging = false }
    }

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

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

See also

More in Gestures and input.