Skip to content

Instantly share code, notes, and snippets.

@cenkbilgen
Last active May 3, 2021 17:00
Show Gist options
  • Save cenkbilgen/c09158fe079d4094f16e83e28e66bda2 to your computer and use it in GitHub Desktop.
Save cenkbilgen/c09158fe079d4094f16e83e28e66bda2 to your computer and use it in GitHub Desktop.
SwiftUI Long Press Progress View
// Using `.updating(...)` with a @GestureState seemed like the way to go
// but pressing state end update is not readily available
// standard `.onLongPress` modifier works best
struct LongPressExample: View {
let duration: TimeInterval
@State private var progress: CGFloat = 0
@State private var isDone = false
var body: some View {
ZStack {
Circle()
.foregroundColor(.purple)
Circle()
.trim(from: 0, to: progress)
.stroke(Color.green, style: StrokeStyle(lineWidth: 5, lineCap: .round))
}
.onLongPressGesture(minimumDuration: duration) { pressing in
print("pressing: \(pressing)")
if pressing {
withAnimation(.easeIn(duration: duration)) {
progress = 1
}
} else {
withAnimation(.easeIn(duration: duration/5.0)) {
progress = 0
}
}
} perform: {
print("done")
progress = 1
isDone = true
}
Text(isDone ? "Done" : "Press Button")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment