Skip to content

Instantly share code, notes, and snippets.

@karthironald
Last active June 25, 2020 04:17
Show Gist options
  • Save karthironald/999db81491e68585fc1d5c98e40114c8 to your computer and use it in GitHub Desktop.
Save karthironald/999db81491e68585fc1d5c98e40114c8 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(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))
}
}
struct CustomLinearProgressViewStyle2: ProgressViewStyle {
var progressColour: Color = .blue
var successColour: Color = .green
var progress: CGFloat
var total: CGFloat
func makeBody(configuration: Configuration) -> some View {
ProgressView(configuration)
.padding(.all, 10)
.background(Color.black.opacity(0.1))
.cornerRadius(10)
.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