dragContainer
A container with draggable views. The drag payload is identifiable. To form the payload, use the identifier of the dragged view inside the container.
Overloads
4 ways to call compose().dragContainer(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().dragContainer({
in?: string | null,
payload: { returns: unknown[]; perform?: (draggedItemID: SwiftUnsupported<'Item.ID'>) => void; },
})nonisolated func dragContainer<Item, Data>(
for itemType: Item.Type = Item.self,
in namespace: Namespace.ID? = nil,
_ payload: @escaping (Item.ID) -> Data
) -> some View
where Item : Transferable, Item : Identifiable, Item == Data.Element, Data : Collection, Item.ID : SendableParameters
options.instring | nulloptions.payload*{ returns: unknown[]; perform?: (draggedItemID: SwiftUnsupported<'Item.ID'>) => void; }A closure which is called when
Return value
A view that can be activated as the source of a drag and drop operation, beginning with user gesture input.
Discussion
Below is an example of a container view with three sections. Each section is draggable. Also, each section is selectable, and each view in a section is selectable as well. When a section drag starts, the app wants to use its custom logic to decide what the payload should be.
If an unselected section is dragged, it should be the only one on the drag payload. If a selected section is dragged, the payload should contain other selected sections, and also the sections that have at least one fruit selected.
var berries: [Fruit]
var citruses: [Fruit]
var tropical: [Fruit]
@State private var selectedSections: [FruitSection.ID]
@State private var selectedFruits: [Fruit.ID]
var body: some View {
ScrollView {
VStack {
BerriesSectionView(FruitSection(berries))
.draggable(containerItemID: FruitSection.berries)
CitrusSectionView(FruitSection(citruses))
.draggable(containerItemID: FruitSection.citruses)
TropicalSectionView(FruitSection(tropical))
.draggable(containerItemID: FruitSection.tropical)
}
}
.dragContainer { (draggedSectionID: FruitSection.ID) in
let identifiers = sectionIDsToDrag(for: draggedSectionID)
return sections(identifiers: identifiers)
}
}
func sectionIDsToDrag(for draggedID: FruitSection.ID) -> [FruitSection.ID] {
// an unselected section is dragged
if !selectedSections.contains(draggedID) { return [draggedID] }
// a selected section is dragged
let payloadIDs = selectedSections + sectionIdentifiersOfSelectedFruits()
return uniqueSections(from: payloadIDs)
}
func sections(identifiers: [FruitSection.ID]) -> [FruitSection] { ... }
func sectionIdentifiersOfSelectedFruits() -> [FruitSection.ID] { ... }
func uniqueSections(from: [FruitSection.ID]) -> [FruitSection.ID] { ... }
struct Fruit: Identifiable { ... }
struct FruitSection: Transferable, Identifiable {
static let berries: UUID
static let citruses: UUID
static let tropical: UUID
let id: UUID
var fruits: [Fruit]
}
struct BerriesSectionView: View {
var section: FruitSection
var body: some View {
HStack {
ForEach(section.fruit) { berry in
BerryView(berry)
}
}
}
}To enable multi-item drag, apply this modifier to a container
view and mark each draggable child with View.draggable(_:)
or View.draggable(containerItemID:containerNamespace:).
a drag operation begins. As an argument, the closure receives the identifier
of the dragged view under the finger or cursor. Using
the passed identifier, put together the payload to drag,
and return from the closure.
Return an empty Collection to disable the drag.
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.
- dragConfigurationConfigures a drag session.
- 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.