Created
November 23, 2019 17:00
-
-
Save a-v-ershov/00efff744a2b306f2fdd18f2714d0e23 to your computer and use it in GitHub Desktop.
DragGesture example
This file contains 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
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