Last active
May 3, 2021 17:00
-
-
Save cenkbilgen/c09158fe079d4094f16e83e28e66bda2 to your computer and use it in GitHub Desktop.
SwiftUI Long Press Progress View
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
// 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