I have been programming an app in SwiftUI which heavily uses the in-built .draggable() and .dropDestination() modifiers.
To enhance the user experience, I would like to make .draggable() work with click+drag rather than long-press+drag (so it is faster to use and less clunky).
I come from Python, where I am used to being able to find system functions and copy them then edit them if I require (under a new function name, of course).
Is it possible to do the same thing with .draggable() and .dropDestination() in SwiftUI, so that I may edit what gesture activates the drag/drop, whilst retaining all other functionality?
Alternatively if I can make an extension to the functions instead that would be an option.
I have looked into writing my own drag/drop gestures, however much of my program relies on the in-built functionality of the .draggable() and .dropDestination() modifiers themselves. This is functionality I have not been able to replicate.
My research on the topic / these functions before coming here has shined little light on the subject, and GPT has not been helpful either.
Minimum reproducible example, simulating the functionality I am currently using drag/drop for.
struct TestView3: View {
@State var ee: String = ""
var body: some View {
VStack {
Text("Drag me")
.draggable("Drag me")
.padding()
Text("to here")
.dropDestination(for: String.self) { droppedObjects, location in
ee = droppedObjects[0]
return true
}
.padding()
Text("You dropped: '\(ee)'")
}
}
}
It has been a bit tough to achieve, and I don't know if it is even worth using. Maybe with some changes it can be scalable, but now it only works with
Strings. I built anextensionand amodifierto make things draggable creating anoverlayto simulate the effect of the defaultdraggablemodifier:Here
dragLocationis used to track where the user's finger or cursor is, whiledragTranslationis used to make the overlayed text move.Here's an example on how you can use it:
Be aware that it definitely needs some work to make it really usable. Now it isn't really flexible. Maybe tomorrow I can work on it a bit more.
Here's the result:
Let me know what do you think of this.