
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - Drag and Drop
SwiftUI provides a special feature known as drag and drop. The drag and drop allows the user to move any item or content from one part or location of the app and drop it to another part or location of the app. Or we are allowed to drag content from one app and drop it to another app. It can drag and drop data, images, text, or other content given in the view.
It is useful in apps like image galleries, file organizers, notes, etc. This feature is available in iOS, iPadOS, and macOS. Apart from the content we can also move, rearrange, reorder or transfer objects such as UI elements or UIViews from different parts of the app by dragging and dropping them.
Drag and Drop using Transferable Items
In SwiftUI, we can drag and drop items with the help of a Transferable protocol. It is the simplest protocol to move elements between the given views. It provides two modifiers; .draggable() and .dropDestination to drag and drop elements from one view to another.
.draggable() Modifier
The .draggable() modifier allows us to perform drag and drop operations in the specified view. This modifier conforms to the Transferable protocol. It allows the user to drag the view using a gesture and it will automatically perform drag internally. We can also combine this modifier with DragGesture() to get more control over the view while dragging.
Syntax
Following is the syntax −
.draggable(_payload: @autoclosure)
Parameter
It takes only one parameter which is the payload. It is a closure which represents the draggable data from the view.
.dropDestination() Modifier
The .dropDestination() modifier specifies the destination of the dropped content. Or we can say that it allows us to specify the location in the view hierarchy where we can drop the draggable items.
Syntax
Following is the syntax −
.dropDestination( for payloadType: Type, action: @escapingClosure, isTargeted: @escapingClosure )
Parameter
for: Represent the view which we want to drop.
action: Represent a closure which takes a dropped element and responds accordingly. It will return true if the drag operation is successful, else it will return false.
isTargeted: Represent a closure which is called when the drag and drop operation enters into the targeted location. It will return true if the cursor is in the specified area, else return false.
Example
The following SwiftUI program to used to perform drag and drop using Transferable Items.
import SwiftUI struct ContentView: View { @State private var language: [String] = ["Java", "C++", "Python", "Go"] @State private var dropIcon = Image(systemName: "square.and.arrow.down") var body: some View { VStack { HStack { ForEach(language, id: \.self) { x in Text(x) .frame(minWidth: 50, minHeight: 50) .background(.white) .foregroundStyle(.pink) .cornerRadius(10) .draggable(x) } } .frame(minWidth: 250, minHeight: 70) .cornerRadius(10) Text("Drop favorite language here:") dropIcon .frame(width: 200, height: 150) .background(.yellow.opacity(0.4)) .foregroundStyle(.white) .dropDestination(for: Image.self) { x, loc in dropIcon = x.first ?? Image(systemName: "square.and.arrow.down") print(loc) return true } } } } #Preview { ContentView() }
Output
