Skip to content

Instantly share code, notes, and snippets.

@heestand-xyz
Last active September 2, 2024 07:37
Show Gist options
  • Save heestand-xyz/311f7f2a239b8e5dfe0f9da9e5f26a46 to your computer and use it in GitHub Desktop.
Save heestand-xyz/311f7f2a239b8e5dfe0f9da9e5f26a46 to your computer and use it in GitHub Desktop.
Cancellable Drag Gesture
import SwiftUI
struct ContentView: View {
@State private var isDragging: Bool = false
@GestureState private var isDraggingActive: Bool = false
var body: some View {
ZStack {
if isDragging {
Color.green
} else {
Color.red
}
}
.gesture(
DragGesture()
.updating($isDraggingActive) { _, isDraggingActive, _ in
isDraggingActive = true
}
.onChanged { value in
if !isDragging {
/// Started
isDragging = true
}
/// Update
}
.onEnded { _ in
/// Ended
isDragging = false
}
)
.onChange(of: isDraggingActive) { _, isDraggingActive in
if isDragging, !isDraggingActive {
/// Cancelled
isDragging = false
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment