Mobile Development 12 min read

Understanding Drag & Drop in iOS: Concepts, APIs, and Implementation

This article explains the iOS Drag & Drop interaction model, covering its core concepts, system integration, multi‑touch features, and step‑by‑step implementation using UIKit APIs such as UIDragInteraction, UIDropInteraction, and NSItemProvider with Swift code examples.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Understanding Drag & Drop in iOS: Concepts, APIs, and Implementation

What is Drag & Drop

Drag & Drop is a graphical data‑interaction method for apps that relies on user drag gestures, allowing quick cut and copy operations for text, images, and other resources.

Overview

Easy to use

Drag & Drop leverages Multiple‑Touch, letting iPad users naturally interact with data by tapping and holding an item then dragging it to a target. Native apps can drag contacts, events, map annotations, and more.

Spring‑loading

Using the Spring‑loading feature, dragging an element onto a button triggers its click action, enabling simple cross‑app data exchange. Dragging to the Dock or home‑screen icon can open the app at a desired location.

Multi‑selection

iOS 11 introduced a shortcut to select multiple items; while dragging one item, tapping other items with another finger adds them to the selection.

System integration

Drag & Drop is integrated throughout iOS and can be used in Home screen, Dock, Reminders, Calendar, Messages, Spotlight, Files, Safari, Contacts, iBooks, News, Notes, Photos, Maps, Keynote, Pages, Numbers, and provides a powerful API for developers.

Goals

Fast response One‑step drag Asynchronous data interaction

Data security Data visible only at final drop Strict source access control

Excellent multi‑touch experience Real‑time UI iOS unified drag integration Intuitive visual feedback Hover navigation Multiple content addition Finger‑to‑finger collaboration Multi‑drag cooperation

Process

Using Drag

Drag works by adding an Interaction layer to a view to receive drag gestures.

This approach is consistent with adding a Gesture operation.

Implementation follows Cocoa's delegate pattern to receive callbacks for each drag state.

import UIKit
let view: UIView = ...
let delegate: UIDragInteractionDelegate = ...
let dragInteraction = UIDragInteraction(delegate: delegate)
view.addInteraction(dragInteraction)

The UIDragInteractionDelegate methods provide multi‑item drag support; when no drag items are present, the gesture is disabled.

Drag items are model objects attached to a view to represent the data being dragged.

Using Drop

Drop can be performed via the pasteConfiguration property of UIResponder , allowing both Drop and Paste interactions.

let config = UIPasteConfiguration(typeIdentifiersForAcceptingClass: NSString.self)
view.pasteConfiguration = config

The paste(itemProviders:) method must be implemented to handle the interaction.

override func paste(itemProviders: [NSItemProvider]) {
    // handle pasted data
}

Drop interaction uses UIDropInteractionDelegate methods to validate data and propose operations (cancel, forbidden, copy, move).

public enum UIDropOperation: UInt {
    case cancel      // cancel transfer
    case forbidden   // temporarily disallow drop
    case copy        // copy data
    case move        // move data within the same app
}

Lift

Lift prepares the drag with preview animation and optional custom UI.

func dragInteraction(_ interaction: UIDragInteraction, previewForLifting item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? {
    guard let view = interaction.view else { return nil }
    let previewView = ParkDragView(image: park.image, name: park.name)
    let parameters = UIDragPreviewParameters()
    parameters.visiblePath = UIBezierPath(roundedRect: previewView.bounds, cornerRadius: 20)
    let target = UIDragPreviewTarget(container: view, center: session.location(in: view))
    return UITargetedDragPreview(view: previewView, parameters: parameters, target: target)
}

Custom previews can replace the default lifted view.

Drag

During the drag phase, itemsForBeginning returns an array of UIDragItem objects.

func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
    let provider = NSItemProvider(object: "Hello World" as NSString)
    let dragItem = UIDragItem(itemProvider: provider)
    return [dragItem]
}

Drop

After validation, performDrop processes the dropped items.

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
    for item in session.items {
        item.itemProvider.loadObject(ofClass: UIImage.self) { (object, error) in
            if let image = object as? UIImage {
                DispatchQueue.main.async {
                    self.imageView.image = image
                }
            }
        }
    }
}

Note that delegate methods run on background threads, so UI updates must be dispatched to the main thread.

Summary

Drag & Drop offers a new data‑exchange experience on iPad, making large‑screen tablets more suitable for work, while iPhone support remains limited to intra‑app dragging.

iOSSwiftDrag and DropUIKitUIDragInteractionUIDropInteraction
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.