dropDestination
Defines the destination of a drag and drop operation that provides a drop operation proposal and handles the dropped content with a closure that you specify.
Declaration
compose().dropDestination<T>({
isEnabled?: boolean,
action: (items: T[], session: SwiftUI.DropSession) => void,
})nonisolated func dropDestination<T>(
for type: T.Type = T.self,
isEnabled: Bool = true,
action: @escaping ([T], DropSession) -> Void
) -> some View
where T : TransferableParameters
options.isEnabledbooleanThe Boolean value indicating if the view accepts drop interactions.
options.action*(items: T[], session: SwiftUI.DropSession) => voidA closure that takes the dropped content and responds appropriately. The first parameter to
actioncontains the dropped items. The second parameter contains the drop session description.
Return value
A view that provides a drop destination for a drop operation of the specified type.
Discussion
The dropped content can be provided as binary data, file URLs, or file promises.
The drop destination is the same size and position as this view.
@State private var isDropTargeted = false
@Binding var isDropEnabled: Bool
var body: some View {
Color.pink
.frame(width: 400, height: 400)
.dropDestination(
for: String.self, isEnabled: isDropEnabled
) { receivedTitles, session in
animateDrop(at: session.location)
process(titles: receivedTitles)
}
}
func process(titles: [String]) { ... }
func animateDrop(at: CGPoint) { ... }See also
More in Drag and drop.
- copyableSpecifies a list of items to copy in response to the system's Copy command.
- cuttableSpecifies an action that moves items to the Clipboard in response to the system's Cut command.
- documentLaunchSubtitleSets the subtitle displayed beneath the title on the document launch card, using a localized string resource.
- documentLaunchTitleSets the title displayed on the document launch card, using a localized string resource.
- draggableActivates this view as the source of a drag and drop operation.
- dropConfigurationConfigures a drop session.
- fileExporterPresents a system dialog allowing the user to export a Transferable item to a file on disk.
- fileExporterFilenameLabelOn macOS, configures the fileExporter with a label for the file name field.
- fileImporterPresents a system dialog for allowing the user to import multiple files.
- fileMoverPresents a system dialog for allowing the user to move an existing file to a new location.
- itemProviderProvides a closure that vends the drag representation to be used for a particular data element.
- pasteDestinationSpecifies an action that adds validated items to a view in response to the system's Paste command.