Skip to content

Instantly share code, notes, and snippets.

@karthironald
Created June 25, 2020 04:20
Show Gist options
  • Save karthironald/b27a9f163e990c2e140d391d49279dbe to your computer and use it in GitHub Desktop.
Save karthironald/b27a9f163e990c2e140d391d49279dbe to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
let timer = Timer.publish(every: 0.25, on: .main, in: .default).autoconnect()
@State private var progress: CGFloat = 0.0
var total: CGFloat = 1.0
var body: some View {
ProgressView(progress >= total ? "Downloaded" : "Downloading...", value: progress, total: total)
.progressViewStyle(CustomLinearProgressViewStyle1(progress: progress, total: total))
.padding(20)
.onReceive(timer) { _ in
if progress < 1 {
self.progress += 0.1
} else {
timer.upstream.connect().cancel()
}
}
}
}
struct CustomLinearProgressViewStyle1: ProgressViewStyle {
var progressColour: Color = .blue
var successColour: Color = .green
var progress: CGFloat
var total: CGFloat
func makeBody(configuration: Configuration) -> some View {
ProgressView(configuration)
.foregroundColor(progress >= total ? successColour : progressColour)
.accentColor(progress >= total ? successColour : progressColour)
.animation(Animation.linear(duration: 1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment