Skip to content

Instantly share code, notes, and snippets.

@dfrobison
Created February 11, 2020 04:01
Show Gist options
  • Save dfrobison/b3361649f5741089c4cab86e0335da24 to your computer and use it in GitHub Desktop.
Save dfrobison/b3361649f5741089c4cab86e0335da24 to your computer and use it in GitHub Desktop.
[Dragging Circle]
import SwiftUI
struct ContentView: View {
// 1.
@State private var currentPosition: CGSize = .zero
@State private var newPosition: CGSize = .zero
var body: some View {
// 2.
Circle()
.frame(width: 100, height: 100)
.foregroundColor(Color.red)
.offset(x: self.currentPosition.width, y: self.currentPosition.height)
// 3.
.gesture(DragGesture()
.onChanged { value in
self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
} // 4.
.onEnded { value in
self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
print(self.newPosition.width)
self.newPosition = self.currentPosition
}
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment