Created
October 17, 2023 04:46
-
-
Save timsneath/78215b7cb89fd489b23552b4afe5912f to your computer and use it in GitHub Desktop.
Small sample of a draggable card object in SwiftUI
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
// 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