I'm trying to animate the progress bar in the SwiftUI progress view. I found something that can help for this but in UIProgressView however I trying to accomplish this with SwiftUI. Current approach:
ProgressView(value: 0.25).animation(Animation.easeInOut(duration: 3))
The problem with this is the whole view is being animated. I want only the progress bar animated.
I found a solution for this
@State private var counter = 0.0
var body: some View {
ProgressView(value: counter, total: 100.0)
.onAppear {
self.runCounter(counter: self.$counter, start: 0.0, end: 50.0, speed: 0.05)
}
}
func runCounter(counter: Binding<Double>, start: Double, end: Double, speed: Double) {
counter.wrappedValue = start
Timer.scheduledTimer(withTimeInterval: speed, repeats: true) { timer in
counter.wrappedValue += 1.0
if counter.wrappedValue == end {
timer.invalidate()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With