Skip to content

Instantly share code, notes, and snippets.

@amosgyamfi
Created November 27, 2024 01:41
Show Gist options
  • Save amosgyamfi/588223b70d8989639f62ac8c26ff9d35 to your computer and use it in GitHub Desktop.
Save amosgyamfi/588223b70d8989639f62ac8c26ff9d35 to your computer and use it in GitHub Desktop.
import SwiftUI
struct DragFromAToB: View {
// State to track the circle's position
@State private var position = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
// State to track the drag gesture's translation
@State private var dragOffset = CGSize.zero
var body: some View {
Circle()
.fill(Color.blue)
.frame(width: 80, height: 80)
.position(position)
.gesture(
DragGesture()
.onChanged { value in
// Update the position while dragging
self.position = CGPoint(x: value.location.x, y: value.location.y)
}
.onEnded { _ in
// Animate back to center when released
withAnimation(.spring(duration: 1, bounce: 0.5)) {
self.position = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
}
}
)
}
}
#Preview {
DragFromAToB()
}
// End of file. No additional code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment