Declaration
compose().dragConfiguration(
configuration: SwiftUI.DragConfiguration
)nonisolated func dragConfiguration(
_ configuration: DragConfiguration
) -> some ViewParameters
configuration*SwiftUI.DragConfigurationA value that describes the configuration
Return value
A view that configures a drag session in a way, described
Discussion
Below is a simplified example of a view that supports copy, move and delete operations for drag.
Drag to delete into trash bin
If a view wants to support drag-to-delete into the trash bin or another location that has similar semantics, it should specify the support for this operation in a drag configuration:
@State private var photos: [Photo] = []
@State private var selectedPhotos: [Photo.ID] = []
var body: some View {
ScrollView {
LazyVGrid(columns: gridColumns) {
ForEach(photos) { photo in
PhotoView(photo: photo)
.draggable(containerItemID: photo.id)
}
}
}
.dragContainer(for: Photo.self) { draggedIDs in
photos(ids: draggedIDs)
}
.dragContainerSelection(selectedPhotos)
.dragConfiguration(DragConfiguration(allowMove: false, allowDelete: true))
.onDragSessionUpdated { session in
if session.phase == .ended(.delete) {
let ids = session.draggedItemIDs(for: Photo.ID.self)
removeAndTrash(ids)
}
}
.dragPreviewsFormation(.stack)
}
func removeAndTrash(_ ids: [Photo.ID]) {
ids.forEach { id
if let idx = photos.firstIndex(where: { $0.id == id }) {
let photo = photos[idx]
photos.remove(at: idx)
try? FileManager.default.trashItem(
at: photo.fileURL, resultingItemURL: nil
)
}
}
}
}Note, that any drag supports copy operation by default. In the snippet above, the view supports both copy and delete operations.
of a drag session.
by the configuration parameter.
See also
More in Gestures and input.
- defaultHoverEffectSets the default hover effect to use for views within this view.
- defersSystemGesturesSets the screen edge from which you want your gesture to take precedence over the system gesture.
- dragContainerA container with draggable views. The drag payload is identifiable. To form the payload, use the identifier of the dragged view inside the container.
- dragContainerSelectionProvides multiple item selection support for drag containers.
- gestureAttaches a gesture to the view with a lower precedence than gestures defined by the view.
- handGestureShortcutAssigns a hand gesture shortcut to the modified control.
- highPriorityGestureAttaches a gesture to the view with a higher precedence than gestures defined by the view.
- hoverEffectApplies a hover effect to this view.
- hoverEffectDisabledAdds a condition that controls whether this view can display hover effects.
- presentationDragIndicatorSets the visibility of the drag indicator on top of a sheet.
- simultaneousGestureAttaches a gesture to the view to process simultaneously with gestures defined by the view.