Skip to content

Instantly share code, notes, and snippets.

@VAndrJ
Created January 8, 2025 11:11
Show Gist options
  • Select an option

  • Save VAndrJ/4cd24d66694acbcec3153f76aafb63d9 to your computer and use it in GitHub Desktop.

Select an option

Save VAndrJ/4cd24d66694acbcec3153f76aafb63d9 to your computer and use it in GitHub Desktop.

Revisions

  1. VAndrJ created this gist Jan 8, 2025.
    42 changes: 42 additions & 0 deletions DraggableMRE.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    import SwiftUI

    struct ContentView: View {
    var body: some View {
    VStack {
    Text("Drag me")
    .font(.largeTitle)
    .padding()
    .frame(maxWidth: .infinity, idealHeight: 200)
    .border(.blue)
    .background(.green.opacity(0.1))
    .draggable(Draggable(identifier: "identifier"))

    Spacer()

    Text("Drop here")
    .font(.largeTitle)
    .frame(width: 200, height: 100)
    .background(.blue.opacity(0.2))
    .dropDestination(for: Draggable.self) { items, location in
    print(items, location)

    return true
    }
    }
    .padding()
    }
    }

    import UniformTypeIdentifiers

    extension UTType {
    static let draggable = UTType(exportedAs: "com.vandrj.DraggableMRE.draggable")
    }

    struct Draggable: Codable, Transferable {
    let identifier: String

    static var transferRepresentation: some TransferRepresentation {
    CodableRepresentation(contentType: .draggable)
    }
    }