Structure

DragGesture

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

Declaration

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 gesture(_:including:) modifier. The following code snippet adds a drag gesture to a Circle and updates the circle’s color, while you perform the drag gesture.

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

Topics

Creating a Drag Gesture

init(minimumDistance: CGFloat, coordinateSpace: CoordinateSpace)

Creates a dragging gesture with the minimum dragging distance before the gesture succeeds and the coordinate space of the gesture’s location.

var minimumDistance: CGFloat

The minimum dragging distance before the gesture succeeds.

var coordinateSpace: CoordinateSpace

The coordinate space of the dragging gesture’s location.

typealias DragGesture.Body

A type that represents the drag gesture and its behavior.

Performing the Gesture

func onChanged((DragGesture.Value) -> Void) -> _ChangedGesture<DragGesture>

Adds an action to perform when the gesture’s value changes.

func onEnded((DragGesture.Value) -> Void) -> _EndedGesture<DragGesture>

Adds an action to perform when the gesture ends.

struct DragGesture.Value

The attributes of a drag gesture.

Composing Gestures

func simultaneously<Other>(with: Other) -> SimultaneousGesture<DragGesture, 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<DragGesture, 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<DragGesture, 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<DragGesture>

Combines a gesture with keyboard modifiers.

Transforming a Gesture

func map<T>((DragGesture.Value) -> T) -> _MapGesture<DragGesture, T>

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

See Also

Basic Gestures

Adding Interactivity with Gestures

Use gesture modifiers to add interactivity to your app.

struct TapGesture

A gesture that recognizes one or more taps.

struct LongPressGesture

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

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.