Skip to content

Instantly share code, notes, and snippets.

@shaps80
Created October 14, 2025 23:13
Show Gist options
  • Select an option

  • Save shaps80/e7841986fbd0ffb8a0d28a1c247ea611 to your computer and use it in GitHub Desktop.

Select an option

Save shaps80/e7841986fbd0ffb8a0d28a1c247ea611 to your computer and use it in GitHub Desktop.
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