Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating processbar in ProgressView SwiftUI

Tags:

ios

swift

swiftui

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.

like image 421
cjay Avatar asked May 26 '26 01:05

cjay


1 Answers

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()
        }
    }
}
like image 184
cjay Avatar answered May 28 '26 15:05

cjay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!