SwiftUI Drag and Drop with Transferable Custom Object

preview_player
Показать описание

Drag an drop in SwiftUI was revamped in iOS 16. It now uses the .draggable and .dropDestination modifiers which require conformance to the new Transferable protocol in Swift. In this video we create a kanban board to teach you how to use Drag and Drop. In the second half of the video I show you how to conform your custom objects to Transferable so you can use them with Drag and Drop.

Starter Project Download:

Human Interface Guidelines - Drag and Drop:

WWDC 2022 - Meet Transferable:

WWDC 2020 - Uniform Type Identifiers:

My iOS Dev Courses:

Twitter:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Paul Hudson's Hacking With Swift:

Donny Wals - Combine:

Mark Moeyken’s SwiftUI Books:

Timestamps:
0:00 - Drag and Drop in iOS 16
0:32 - Starter Project Overview
1:21 - Strings vs. Custom Object
1:52 - Draggable
4:25 - Drop Destination
7:21 - Drag and Drop copy by default
7:51 - Remove tasks from starting array
9:03 - Remove duplicates from destination
10:43 - Highlight drop target on hover
13:28 - Drag and Drop basics review
17:01 - Create Custom Object
18:18 - Conform to Transferable
20:06 - Custom Uniform Type Identifier
22:23 - Adapt code with new Transferable type
25:39 - Wrong drop target type example
26:25 - Did you enjoy that?

#swift #softwaredeveloper #iosdeveloper
Рекомендации по теме
Комментарии
Автор

Man, thank you for existing, you keep saving my life, currently, I'm unemployed so every dime counts, but as soon as I manage to get a job in swiftUI I want to become a member of your channel and recommend it to every single person ill know.

blytte
Автор

Pretty much the perfect video Sean. So well explained as usual

StewartLynch
Автор

Clearest example of implementation of Transferable Protocol I've seen. Thanks.

gilbertgonsalves
Автор

Great Tutorial! Could you make a tutorial with this but you add a + button which adds a block that you can drag?

FIightAviator
Автор

Great tutorial Sean. Drag and Drop has always been a bit of a mystery to me. Cheers man.

chrispyk
Автор

Been waiting for this video for a long time! Thank you. Just purchased the widget course as well.

AK
Автор

Hi Sean, Thank you for the video. I have tried this tutorial but my preview view is going on somewhere middle of the view after dropping item. Can you please me on why it's behaving like this. tried on both iOS and VisionOS simulator. Thanks in advance.

payalmalekar
Автор

First of all, thanks for in-depth guide. Helped a lot to understand a topic.
I have a question thought: is there any way to tweak drag gesture parameters?
I have a huge delay, trying to drag an item, that is inside a ScrollView - would like to reduce it

zykis
Автор

Great Allen Thanks for sharing your knowledge !!

rahuljamba
Автор

My life is divided into 2 parts, before and after this tutorial

soheilpakgohar
Автор

Would love to see the next video from your „built ship provit“ series. How is it going with creator view?

Sengardisback
Автор

Has anybody tried this? I haven't gotten it to work. Did Apple break this? I can get a list Item to drag but I don't get the green circle with the + sign and it won't drop. I put print() statements in the .dropDestination closures and the print0 in the first closure for processing the dragged item(s) doesn't fire. The one in the isTargeted does fire when the drag goes over the drop destination.

chuckincharlie
Автор

Great tutorial, as usual. However, I rebuilt this with Mac as target and the .dropDestination doesn't work. I can drag the Text but the Kanban doesn't light up. Any ideas why? Same exact code works fine on iPad target.

JackCSwift
Автор

Sean, thank you very much. I have a question which goes deeper.

extension ImageDocumentLayer: Transferable {
static var transferRepresentation: some TransferRepresentation {
.layer) { layer in
layer.data()
} importing: { data in
try ImageDocumentLayer(data: data)
}
.png) { layer in
layer.pngData()
}
}
}

This is code is on Apple Official Doc. This transferRepresentation consists of two DataRepresentation. They are async throw functions. Why transferRepresentation itself doesn't have the key word async or throws?

alexl
Автор

Does anyone else have a bug with the preview of the drag not disappearing properly with iOS 18?

slck
Автор

Is there I can make a way to make an element containing multiple other elements draggable? I have a complex reusable component with multiple textfields in it that I want to be able to click and drag the entire set of textfields within the main view.

matthewbrignola
Автор

Can we remove the green plus icon when it is dragged??

AlekhyaBatchu
Автор

Brilliant! Thank you for the good content :)

discodolken
Автор

Should this only be used for dropping items between containers? What if I want to reorder the items within the container?

cakeezz
Автор

Hey Sean, just a quick question: it is already been 4 years since SwiftUI has been released. My question is, is it still the case that UIKIT is more advanced than SwiftUI? For Rex maple, changing a placeholder holder in SwiftUI vs UIKIT? Or is SwiftUI now just as advanced as UIKIT?

AkimboFennec