Declaration
compose().dragContainerSelection<ItemID>(
selection: ItemID[],
options?: {
containerNamespace?: string | null;
}
)nonisolated func dragContainerSelection<ItemID>(
_ selection: @autoclosure @escaping () -> Array<ItemID>,
containerNamespace: Namespace.ID? = nil
) -> some View
where ItemID : Hashable, ItemID : SendableParameters
selection*ItemID[]A closure that provides identifiers of selected items.
options.containerNamespacestring | nullAn optional namespace of the drag container.
Discussion
A drag container finds the nearest enclosing dragContainerSelection(_:containerNamespace:)
with the same item identifier type and same namespace, if specified.
Drag container uses the provided selected item identifiers to determine what
the drag payload should be.
If the dragged view is associated with a selected identifier, the payload should contain
all the selected items. If the dragged view is not selected, the payload should
not contain the whole selection, just the dragged item.
With dragContainerSelection(_:containerNamespace:), you get fine-grained control
over what items are included in the drag payload.
struct FruitContainer: View {
@State private var fruits: [Fruit]
@State private var selection: [Fruit.ID]
var body: some View {
VStack {
ForEach(fruits) { fruit in
FruitView(fruit)
.draggable(containerItemID: fruit.id)
}
}
.dragContainer(for: Fruit.self) { ids in
fruits(with: ids)
}
.dragContainerSelection(selection)
}
func fruits(with ids: [Fruit.ID]) -> [Fruit] { ... }
struct Fruit: Transferable, Identifiable {
let id: String
...
}
struct FruitView: View {
init(_ fruit: Fruit) { ... }
}
}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.
- dragContainerA container with draggable views. The drag payload is identifiable. To form the payload, use the identifier of the dragged view inside the container.
- 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.