Skip to content
iamjoaovytorPublic

About

A drag-and-drop library for SwiftUI with native iOS drag interactions.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

Repository files navigation

SwiftDrag

Native drag-and-drop for SwiftUI lists on iOS, powered by UIDragInteraction.

  • Flat lists (SwiftDragView)
  • Sectioned lists with cross-section dragging (SwiftDragSectionedView)
  • Native iOS drag previews and animations
  • Haptic feedback (respects Reduce Motion)
  • Configurable corner radius, restore delay, and UTTypes

Requirements

  • iOS 16+
  • Swift 5.9+

Installation

Swift Package Manager

https://github.com/iamjoaovytor/SwiftDrag.git

Or add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/iamjoaovytor/SwiftDrag.git", from: "1.0.0")
]

Demo

Flat List Sectioned List
Flat list drag and drop Sectioned list drag and drop

Quick Start

1. Define your model

import SwiftDrag

struct Todo: SwiftDragItem {
    let id: UUID
    var title: String
}

That's it. isDraggable defaults to true and getItemProvider() returns an empty provider.

2. Flat list

@State private var todos: [Todo] = [
    Todo(id: UUID(), title: "Buy groceries"),
    Todo(id: UUID(), title: "Walk the dog"),
    Todo(id: UUID(), title: "Read a book"),
]

var body: some View {
    ScrollView {
        LazyVStack(spacing: 8) {
            SwiftDragView(items: $todos) { todo in
                // Your card view
                HStack {
                    Image(systemName: "line.3.horizontal")
                        .foregroundStyle(.tertiary)
                    Text(todo.title)
                    Spacer()
                }
                .padding()
                .background(Color(.secondarySystemGroupedBackground))
                .clipShape(RoundedRectangle(cornerRadius: 10))
            } dropView: { _ in
                // Placeholder shown while dragging
                RoundedRectangle(cornerRadius: 10)
                    .strokeBorder(style: StrokeStyle(lineWidth: 2, dash: [6, 4]))
                    .foregroundStyle(.quaternary)
                    .frame(height: 50)
            } dropCompleted: {
                print("Reordered!")
            }
        }
        .padding()
    }
}

3. Sectioned list

struct TodoSection: SwiftDragSection {
    let id: UUID
    var title: String
    var items: [Todo]
}

@State private var sections: [TodoSection] = [
    TodoSection(id: UUID(), title: "To Do", items: [
        Todo(id: UUID(), title: "Setup project"),
        Todo(id: UUID(), title: "Write tests"),
    ]),
    TodoSection(id: UUID(), title: "Done", items: [
        Todo(id: UUID(), title: "Define architecture"),
    ]),
]

var body: some View {
    ScrollView {
        LazyVStack(spacing: 0) {
            SwiftDragSectionedView(sections: $sections) { section in
                // Section header
                Text(section.title)
                    .font(.headline)
                    .padding()
            } card: { todo in
                // Item card
                Text(todo.title)
                    .padding()
                    .background(Color(.secondarySystemGroupedBackground))
                    .clipShape(RoundedRectangle(cornerRadius: 8))
                    .padding(.horizontal)
                    .padding(.bottom, 6)
            } dropView: { _ in
                RoundedRectangle(cornerRadius: 8)
                    .strokeBorder(style: StrokeStyle(lineWidth: 2, dash: [6, 4]))
                    .foregroundStyle(.quaternary)
                    .frame(height: 40)
                    .padding(.horizontal)
            } dropCompleted: {
                print("Sections updated!")
            }
        }
    }
}

Full Example

Check out Examples/SwiftDragExample.swift for a ready-to-run demo with both flat and sectioned lists. Just copy it into your project.

Configuration

// Drag preview corner radius (default: 12)
.environment(\.dragPreviewCornerRadius, 16)

// Delay before restoring the original view after lift (default: 0.2s)
.environment(\.dragRestoreDelay, 0.3)

// UTTypes for external drop support (default: [] = internal only)
.environment(\.dragSupportedUTTypes, [.text])

Making items non-draggable

struct Todo: SwiftDragItem {
    let id: UUID
    var title: String
    var isDraggable: Bool = true  // set to false to lock an item
}

License

MIT

About

A drag-and-drop library for SwiftUI with native iOS drag interactions.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages