Skip to content

Instantly share code, notes, and snippets.

@timsneath
Created October 17, 2023 04:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsneath/78215b7cb89fd489b23552b4afe5912f to your computer and use it in GitHub Desktop.
Save timsneath/78215b7cb89fd489b23552b4afe5912f to your computer and use it in GitHub Desktop.
Small sample of a draggable card object in SwiftUI
// Adapted from Hacking with SwiftUI, credit to Paul Hudson (@twostraws).
// https://www.hackingwithswift.com/books/ios-swiftui/animating-gestures
import SwiftUI
struct ContentView: View {
@State private var dragAmount = CGSize.zero
var body: some View {
RoundedRectangle(cornerRadius: 10)
.foregroundStyle(LinearGradient(gradient: Gradient(colors: [.yellow, .red]),
startPoint: .topLeading, endPoint: .bottomTrailing))
.frame(width: 300, height: 200)
.shadow(radius: 10, x: 10, y: 10)
.offset(dragAmount)
.gesture(DragGesture()
.onChanged { dragAmount = $0.translation }
.onEnded { _ in
withAnimation(.spring()) { dragAmount = .zero }
}
)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment