Skip to content

Instantly share code, notes, and snippets.

@karthironald
Created June 25, 2020 04:24
Show Gist options
  • Save karthironald/363ca6286470db78a8724418041ee9f2 to your computer and use it in GitHub Desktop.
Save karthironald/363ca6286470db78a8724418041ee9f2 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, label: {
HStack {
Button(action: {
self.progress -= 0.1
}, label: {
Image(systemName: "sun.min.fill")
.resizable()
.frame(width: 30, height: 30)
.aspectRatio(contentMode: .fit)
})
Spacer()
Button(action: {
self.progress += 0.1
}, label: {
Image(systemName: "sun.max.fill")
.resizable()
.frame(width: 30, height: 30)
.aspectRatio(contentMode: .fit)
})
}
})
.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