Contents

DragGesture

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

Declaration

struct DragGesture

Mentioned in

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.

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)
    }
}

Topics

Creating a drag gesture

Deprecated initializers

Structures

Initializers

See Also

Recognizing gestures that change over time