SwiftUI Drag and Drop with Transferable Custom Object

  Рет қаралды 24,086

Sean Allen

Sean Allen

Күн бұрын

Пікірлер: 84
@seanallen
@seanallen Жыл бұрын
Go deeper into SwiftUI and UIKit with my iOS Developer courses at seanallen.teachable.com
@blytte
@blytte 7 ай бұрын
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.
@seanallen
@seanallen 7 ай бұрын
I appreciate that. Happy my channel is helpful for ya
@StewartLynch
@StewartLynch Жыл бұрын
Pretty much the perfect video Sean. So well explained as usual
@seanallen
@seanallen Жыл бұрын
I appreciate that. Means a lot coming from you, Stewart.
@gilbertgonsalves9815
@gilbertgonsalves9815 Ай бұрын
Clearest example of implementation of Transferable Protocol I've seen. Thanks.
@FIightAviator
@FIightAviator 9 ай бұрын
Great Tutorial! Could you make a tutorial with this but you add a + button which adds a block that you can drag?
@AK907
@AK907 Жыл бұрын
Been waiting for this video for a long time! Thank you. Just purchased the widget course as well.
@seanallen
@seanallen Жыл бұрын
Glad you liked the video and hope you enjoy the Widgets course. That's a fun one.
@soheilpakgohar6630
@soheilpakgohar6630 Жыл бұрын
My life is divided into 2 parts, before and after this tutorial
@seanallen
@seanallen Жыл бұрын
Lol, glad you liked it.
@sl1ck64
@sl1ck64 2 ай бұрын
Does anyone else have a bug with the preview of the drag not disappearing properly with iOS 18?
@istvansipos3121
@istvansipos3121 2 ай бұрын
Yes, having the same problem and couldn't find a workaround. The same code works fine on iOS 17.
@chrispy104k
@chrispy104k Жыл бұрын
Great tutorial Sean. Drag and Drop has always been a bit of a mystery to me. Cheers man.
@seanallen
@seanallen Жыл бұрын
Happy to help!
@alexl2512
@alexl2512 10 ай бұрын
Sean, thank you very much. I have a question which goes deeper. extension ImageDocumentLayer: Transferable { static var transferRepresentation: some TransferRepresentation { DataRepresentation(contentType: .layer) { layer in layer.data() } importing: { data in try ImageDocumentLayer(data: data) } DataRepresentation(exportedContentType: .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?
@payalmalekar6529
@payalmalekar6529 Ай бұрын
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.
@Sengardisback
@Sengardisback Жыл бұрын
Would love to see the next video from your „built ship provit“ series. How is it going with creator view?
@seanallen
@seanallen Жыл бұрын
I wanted to get that out this week, but it's looking like next week. I'm working on the next release and will do a video after I ship that.
@rahuljamba5846
@rahuljamba5846 7 ай бұрын
Great Allen Thanks for sharing your knowledge !!
@seanallen
@seanallen 7 ай бұрын
No problem :)
@zykis999
@zykis999 Жыл бұрын
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
@JackCSwift
@JackCSwift 5 ай бұрын
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.
@matthewbrignola5510
@matthewbrignola5510 Ай бұрын
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.
@catsCollection
@catsCollection 7 ай бұрын
it works on iOS but on macOS it is being dragged but not accepting it to be dropped, please help
@chuckincharlie
@chuckincharlie Жыл бұрын
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.
@seanallen
@seanallen Жыл бұрын
I just checked the project and rebuilt it with the latest Xcode and iOS 17 and the project is working. Are you following my project exactly, or trying to implement it into your existing code?
@chuckincharlie
@chuckincharlie Жыл бұрын
@@seanallen Thanks for checking it for me. I'm kind of doing both. I have an app I'm building that uses AppleMusic and I'm building a playlist builder for it. A list of songs on one side and the playlist(an empty list initially) on the other side and I want to drag songs onto the playlist. I built a simplified separate project to implement basically what you're doing for quick reference on other projects but couldn't get it to work. That project uses the two lists though which I think is the problem. Someone on Reddit iOSProgramming said to use onDrag and onDrop when working with lists. Interesting thing is I threw print() statements into both halves of the .dropDestination and the first print() in the top section doesn't fire but the second Print() in the isTargeted does. I also was primarily building for iPad and got an error off the drag (Found no UIEvent for backing event of type: 11; contextId: 0xaabebfd) and I tried it on iPhone and didn't get the error but it still didn't work. Added primarily in case this might help someone else.
@pro100filipp
@pro100filipp Жыл бұрын
​@@seanallen I have the same problem - but in my case it does not work at all. Step to reproduce: - download Xcode project from the link in the description - try to drag and drop using Preview - nothing happens, it just does not respond to my gestures - build on my iPhone 12 mini and try to drag and drop - still nothing happens, it just does not respond to my gestures I have Xcode Version 15.0.1 (15A507), Swift version 5.9, macOS version 14.1.1 Same problem goes for any tutorial projects using draggable/dropDestination and also for my own code
@thaisrobba6449
@thaisrobba6449 11 ай бұрын
@@pro100filipp Seems to be a problem for apps that also target macOs. I couldn't for the life of me understand what was different from the tutorial and my project, so I downloaded Sean's starter project and yep, adding mac as a build target breaks it 🫠
@Mailliwi2
@Mailliwi2 10 ай бұрын
+1 on this. Also trying to use this with on a Mac target and it doesn't seem to be letting me use my own models. Works fine with [String] but not with custom models. To be more precise, I can drag the custom views fine, but cannot drop them in the specified drop destination. I am not using the previews, as they don't work for the drag and drop functionality at the time of this message. Pretty confident I've set up everything properly, including setting up the custom UTType in the project's info.plist. Will continue investigating.
@garya1809
@garya1809 4 ай бұрын
Hopefully not too nitpicky but if you redo this video or do something similar in the future, can you call it something other than task? a Task class in C# or like a Task/FutureTask in Java mean something very different than a chore/todo and I'm sure other languages use Task as like a promise/future event, and while I watched this I kept saying stuff "but where is the task callback?" or "wait is the variable inProgressTask for a cancellable promise?" IDK - just some random thoughts.
@AlekhyaBatchu
@AlekhyaBatchu 3 ай бұрын
Can we remove the green plus icon when it is dragged??
@DaniNotHere
@DaniNotHere 2 ай бұрын
Did you solve it?
@chuckincharlie
@chuckincharlie Жыл бұрын
How much of the boilerplate of conforming to Transferable will Apple make go away in the next year or whatever.
@cakeezz
@cakeezz 8 ай бұрын
Should this only be used for dropping items between containers? What if I want to reorder the items within the container?
@seanallen
@seanallen 8 ай бұрын
I believe this will still work, you just need to add the logic when the item is dropped to re-order the items in each array
@isaacboothe9399
@isaacboothe9399 Жыл бұрын
Gold. Thank you!
@seanallen
@seanallen Жыл бұрын
You're welcome!
@AkimboFennec
@AkimboFennec Жыл бұрын
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?
@seanallen
@seanallen Жыл бұрын
If you need to do VERY HIGHLY customized UI, then UIKit is still the way to go. You have a lot more control. That being said, SwiftUI gets better and better each year and is Apple's future.
@AkimboFennec
@AkimboFennec Жыл бұрын
@@seanallen so you say that UIKIT is still more advanced though. Thank you
@dre5671
@dre5671 Жыл бұрын
@@AkimboFennecnote that, not only can you implement UIKit in your SwiftUI project, but Apple has recently simplified it, and I’m sure will continue to simplify.
@thealeos
@thealeos Жыл бұрын
Is it possible to hide an item that is being dragged?
@cheesable
@cheesable Жыл бұрын
when I try to drag after adding draggable, the object kinda pops up but doesnt move. do you know what is the issue?
@septimir5278
@septimir5278 11 ай бұрын
Also experiencing this issue.
@septimir5278
@septimir5278 11 ай бұрын
Cross-referencing my solution below: "for future reference to others encountering this issue: The template is OK. The drag and drop gesture must be attempted in the simulator, not the preview. I guess the behavior between these two is not identical, so beware."
@cheesable
@cheesable 11 ай бұрын
@septimir5278 thanks. That's an easy fix. Hmmm. I'll go try it out!
@discodolken
@discodolken Жыл бұрын
Brilliant! Thank you for the good content :)
@seanallen
@seanallen Жыл бұрын
Glad you enjoy it!
@AndrewDChristie
@AndrewDChristie Жыл бұрын
Thanks!
@TImHagele
@TImHagele Жыл бұрын
any tips on where to start if i want to add pictures to my UTType ? Does not conform to Hashable / Codeable... struct menuItem: Codable, Hashable, Transferable { let id: UUID let itemName: String let summary: String let ingrediants: String let extras: String let demoPicture: Image? let fullSizePicture: Image? static var transferRepresentation: some TransferRepresentation { CodableRepresentation(contentType: .developerType) // DataRepresentation(contentType: .developerTask) // FileRepresentation(contentType: .developerTask) } }
@TheMcGloneCode
@TheMcGloneCode Жыл бұрын
Hey how would you make a HStack in a scroll view but the user can change the order of the items within? Can you use .draggable?
@VladimirKim-cl3rh
@VladimirKim-cl3rh Жыл бұрын
Thank you very much!!!
@seanallen
@seanallen Жыл бұрын
You're welcome!
@ios3946
@ios3946 Жыл бұрын
@seanallen how can I detect which view is the one being dragged? perhaps for changing the color of the destination one.. .draggable doesn't offer any callbacks :(
@zykis999
@zykis999 Жыл бұрын
.draggable modifier has a preview parameter for such purpose
@septimir5278
@septimir5278 11 ай бұрын
I believe the template no longer works out of the box. Adding `.draggable(task)` in step one allows the object to pop up on hold click, but it cannot be dragged at all--it is stuck in place (it cannot even be dragged to a `.dropDestination`, which is added later). There is no build error. Can someone explain why this behavior is occurring? Someone else in the comments is also experiencing this.
@septimir5278
@septimir5278 11 ай бұрын
All right, so, for future reference to others encountering this issue: The template is OK. The drag and drop gesture must be attempted in the simulator, not the preview. I guess the behavior between these two is not identical, so beware.
@yugasamuel6402
@yugasamuel6402 11 ай бұрын
Hey everyone! Does anyone know how to remove the delay when we want to drag our views? Your help would be much appreciated! Thanks in advance!
@kavinda_dilshan_paramsoodi
@kavinda_dilshan_paramsoodi Жыл бұрын
Loved It....
@seanallen
@seanallen Жыл бұрын
Happy to hear it!
@alejmc
@alejmc Жыл бұрын
And I’m sure I’m gonna love it!😅 EDIT: well, yes I did. Great tutorial
@HeyNoah
@HeyNoah Жыл бұрын
Super awesome!
@seanallen
@seanallen Жыл бұрын
Glad you liked it!
@dmitrygolovin8066
@dmitrygolovin8066 Жыл бұрын
Great video!
@victorriurean
@victorriurean Жыл бұрын
@shaggyfromscobbydooo2572
@shaggyfromscobbydooo2572 10 ай бұрын
changing com.public.data to public.data helped to fixed a iOS on macOS fyi
@sonn.andrey
@sonn.andrey 25 күн бұрын
Amazing, thank you for this.
@andresp1582
@andresp1582 Жыл бұрын
Awesome app
@seanallen
@seanallen Жыл бұрын
Glad you liked it!
@richprice5434
@richprice5434 Жыл бұрын
Didn’t Kavsoft build this exact thing a few days ago ??
@seanallen
@seanallen Жыл бұрын
Yes, but a few days ago I tweeted a sneak preview video of this tutorial and that I would be posting later this week. That tweet went up ~6 hours before their video went live. We talked it out on Twitter as coincidental timing. My Tweet - twitter.com/seanallen_dev/status/1682078941857587202
@richprice5434
@richprice5434 Жыл бұрын
@@seanallen interesting
@VijayarajSuyambu-he9jc
@VijayarajSuyambu-he9jc 3 ай бұрын
subscribed
@kunaldhopavkar8685
@kunaldhopavkar8685 3 ай бұрын
My code was working unitl i change the exportedAs parameter for UTType with new bundle identifer and solution why it break and how to fix it ? @seanallen
@AndrewDChristie
@AndrewDChristie Жыл бұрын
Thanks!
@seanallen
@seanallen Жыл бұрын
Happy to help and I appreciate the generosity, Andrew!
@bbz9002
@bbz9002 3 ай бұрын
Thanks!!
@seanallen
@seanallen 3 ай бұрын
No problem!
SwiftUI Grids - LazyVGrid, LazyHGrid, Static Grid
17:55
Sean Allen
Рет қаралды 14 М.
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 23 МЛН
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 22 МЛН
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 7 МЛН
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 5 МЛН
SwiftUI Bar Chart with Customizations | Swift Charts
21:26
Sean Allen
Рет қаралды 27 М.
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
New SwiftUI Photo Picker - Single & Multiple Selection
12:42
Sean Allen
Рет қаралды 11 М.
Play: The Secret Weapon for iOS Devs & Designers
10:12
Sean Allen
Рет қаралды 36 М.
SwiftUI List with Custom Cell & Passing Data
36:27
Sean Allen
Рет қаралды 118 М.
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 23 МЛН