Last active
November 23, 2019 12:15
-
-
Save a-v-ershov/6b1fb40735171b7ee0a83d40128a9c4f to your computer and use it in GitHub Desktop.
Пример DragGesture
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
struct DragGestureExample: View { | |
@State var rectangleOffset: CGSize = .zero | |
var body: some View { | |
// Создаем DragGesture | |
let dragGesture = DragGesture() | |
// При изменение локации мы пересчитываем значения отступа для прямоугольника | |
.onChanged { value in | |
self.rectangleOffset = value.translation | |
} | |
// После завершения жеста прямоугольник возвращается на место | |
.onEnded { _ in | |
self.rectangleOffset = .zero | |
} | |
return Rectangle() | |
.foregroundColor(.green) | |
.cornerRadius(40) | |
// Изменяем местоположение прямоугольника | |
.offset(rectangleOffset) | |
.frame(width: 200, height: 100, alignment: .center) | |
// Добавляем gesture к view | |
.gesture(dragGesture) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment