Created
October 14, 2025 23:13
-
-
Save shaps80/e7841986fbd0ffb8a0d28a1c247ea611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension View { | |
| func draggable(_ location: Binding<CGPoint>) -> some View { | |
| modifier(DragModifier(location: location)) | |
| } | |
| } | |
| struct DragModifier: ViewModifier { | |
| @GestureState private var startLocation: CGPoint? = nil | |
| @Binding var location: CGPoint | |
| func body(content: Content) -> some View { | |
| content | |
| .offset(x: location.x, y: location.y) | |
| .gesture( | |
| DragGesture(minimumDistance: 0) | |
| .onChanged { value in | |
| var newLocation = startLocation ?? location | |
| newLocation.x += value.translation.width | |
| newLocation.y += value.translation.height | |
| self.location = newLocation | |
| }.updating($startLocation) { (value, startLocation, transaction) in | |
| startLocation = startLocation ?? location | |
| } | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment