DragGesture
A dragging motion that invokes an action as the drag-event sequence changes.
Declaration
type DragGesture = { minimumDistance?: number; coordinateSpace?: CoordinateSpaceProtocol; } | { minimumDistance?: number; coordinateSpace?: CoordinateSpaceProtocol; inputKinds?: GestureInputKinds; };@MainActor @preconcurrency struct DragGestureOverview
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.
- AutomaticHoverEffectThe default hover effect based on the surrounding context.
- CustomHoverEffectA type that represents how a view should change when a pointer hovers over a view, or when someone looks at the view.
- DragConfigurationThe behavior of the drag, proposed by the dragging source. A value that describes the drag operations a drag source supports.
- DragConfigurationOperationsOutsideAppDescribes the suggested drag operations to other applications.
- DragConfigurationOperationsWithinAppDescribes the drag operations suggested to destinations within the app.
- DragGestureValueThe attributes of a drag gesture.
- DragSessionDescribes the ongoing dragging session.
- DragSessionIDThe identifier of a drag session.
- DragSessionPhaseThe phase of the current drag session
- GestureAn instance that matches a sequence of events to a gesture, and returns a stream of values for each of its states.
- GestureChainGesture with chained calls: SwiftUI.Gesture.exclusively(...).
- GestureInputKindsAn option set that specifies which input kinds a gesture should recognize.