Beyond Button, SwiftUI exposes gestures as values you compose. TapGesture, LongPressGesture and DragGesture can be attached with .gesture(), combined in sequence or simultaneously, and given @GestureState for the transient data that should reset automatically when the finger lifts.
Animation comes in two forms and the distinction is worth getting right. Implicit animation - .animation(_:value:) - says "whenever this particular value changes, animate the difference". Explicit animation - withAnimation { } - says "whatever changes as a result of running this block, animate it". Reach for implicit when one property drives one visual change, and explicit when a single user action changes several things at once.
Always scope an implicit animation to a value. The old parameterless .animation() animated every change to that view, including ones you never intended, and is deprecated for exactly that reason.
Two things make animation feel native rather than decorative: use spring animations, which are the system default and match how the rest of iOS moves; and respect Reduce Motion, which you can read from the environment.
import SwiftUI
struct DraggableCard: View {
@State private var isExpanded = false
@State private var offset: CGSize = .zero
@GestureState private var isPressing = false
@Environment(\.accessibilityReduceMotion) private var reduceMotion
var body: some View {
VStack {
RoundedRectangle(cornerRadius: 20)
.fill(.tint)
.frame(width: isExpanded ? 280 : 160, height: isExpanded ? 180 : 160)
.scaleEffect(isPressing ? 0.95 : 1)
.offset(offset)
// Implicit: tied to the values that drive it.
.animation(reduceMotion ? nil : .spring(duration: 0.35), value: isExpanded)
.animation(.interactiveSpring, value: offset)
.gesture(
DragGesture()
.onChanged { offset = $0.translation }
.onEnded { _ in
// Explicit: several things settle together.
withAnimation(.spring) { offset = .zero }
}
)
.gesture(
LongPressGesture(minimumDuration: 0.2)
.updating($isPressing) { current, state, _ in state = current }
)
.onTapGesture { isExpanded.toggle() }
Text(isExpanded ? "Tap to shrink" : "Tap to grow, drag to move")
.font(.footnote).foregroundStyle(.secondary).padding(.top, 24)
}
}
}
#Preview { DraggableCard() }