Skip to content

Instantly share code, notes, and snippets.

@ershovio
Created November 23, 2019 17:00
Show Gist options
  • Save ershovio/00efff744a2b306f2fdd18f2714d0e23 to your computer and use it in GitHub Desktop.
Save ershovio/00efff744a2b306f2fdd18f2714d0e23 to your computer and use it in GitHub Desktop.
DragGesture example
struct DragGestureExample: View {
@State var rectangleOffset: CGSize = .zero
var body: some View {
// DragGesture creation
let dragGesture = DragGesture()
// When drag location is changed we recalculate offset for the rectangle
.onChanged { value in
self.rectangleOffset = value.translation
}
// When gesture ended we return the rectangle to the initial position
.onEnded { _ in
self.rectangleOffset = .zero
}
return Rectangle()
.foregroundColor(.green)
.cornerRadius(40)
// Change position for the rectangle
.offset(rectangleOffset)
.frame(width: 200, height: 100, alignment: .center)
// Add the dragGesture to this view
.gesture(dragGesture)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment