@State private var progress: Double = 0.0
let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
var body: some View {
ProgressView(value: progress, total: 100,
label: {
Text("Downloading...")
.padding(.bottom, 4)
}, currentValueLabel: {
Text("\(Int(progress))%")
.padding(.top, 4)
}
).progressViewStyle(.linear)
.onReceive(timer) { _ in
if progress < 100 {
progress += 1
} else {
progress = 0
}
}
}
And right not the style is not right, consider change the code to this :
//
// ContentView.swift
// ProgressIconandBar
//
// Created by WilliamJiamin on 2024/6/29.
//
import SwiftUI
struct ContentView: View {
@State private var progress: Double = 0.0
let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Spacer()
// Circular Progress View
ProgressView("Downloading...", value: progress, total: 100)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
// .scaleEffect(2)
Spacer()
// Linear Progress View
ProgressView(value: progress, total: 100) {
Text("Downloading...")
.padding(.bottom, 4)
.frame(maxWidth: .infinity, alignment: .center)
} currentValueLabel: {
Text("\(Int(progress))%")
.padding(.top, 4)
.frame(maxWidth: .infinity, alignment: .center)
}
.progressViewStyle(.linear)
.padding(10)
.frame(maxWidth: .infinity, alignment: .center)
Spacer()
}
.onReceive(timer) { _ in
if progress < 100 {
progress += 1
} else {
progress = 0
}
}
}
}
#Preview {
ContentView()
}