Progress view in SwiftUI is used to indicate the “progress” toward a task completion 😃
Progress view comes in two flavors 😋 there is a version which show indeterminate progress as shown below:
struct ProgressViewExample: View {
var body: some View {
ProgressView("Loading... Please wait.")
}
}
Other version of Progress view shows percent completion. We will use a state variable called progress . Will use Slider view to change progress variable’s value. We will also use progress view’s label to show overall completion percentage.
Progress view comes with two built-in styles, circular and linear progress style. If you have progress view initialized with a value then linear style becomes default for progress view. If only label is defined then circular progress view is created.
We can even extend progress view style protocol with our own custom implementation. We will implement circular progress bar with progress percentage at the center of the circle.
public struct CircularProgressViewStyle: ProgressViewStyle {
var size: CGFloat
private let lineWidth: CGFloat = 20
private let defaultProgress = 0.0
private let gradient = LinearGradient(colors: [.purple, .blue], startPoint: .leading, endPoint: .trailing)
public func makeBody(configuration: ProgressViewStyleConfiguration) -> some View {
ZStack {
configuration.label
progressCircleView(fractionCompleted: configuration.fractionCompleted ?? defaultProgress)
configuration.currentValueLabel
}
}
private func progressCircleView(fractionCompleted: Double) -> some View {
Circle()
.stroke(gradient, lineWidth: lineWidth)
.opacity(0.2)
.overlay(progressFill(fractionCompleted: fractionCompleted))
.frame(width: size, height: size)
}
private func progressFill(fractionCompleted: Double) -> some View {
Circle()
.trim(from: 0, to: CGFloat(fractionCompleted))
.stroke(gradient, lineWidth: lineWidth)
.frame(width: size)
.rotationEffect(.degrees(-90))
}
}
With that we have reached the end of this article. Thank you once again for reading. If you liked this, don’t forget to subscribe our weekly newsletter at https://www.devtechie.com