HowTo —— SwiftUI2.0 使用ProgressView显示进度条
东坡肘子 6 人赞同了该文章SwiftUI2.0 新增了一些便捷的内置控件,比如说Label、ProgressView等。其基本形态都很普通,不过都支持自定义style。官方的意图也比较明显,通过内置控件,规范代码、提高原型编写速度,如需要更精细控制可通过扩展style来完成。
经典小菊花
ProgressView()
线性进度条
ProgressView("完成量", value: 50, total: 100)
代码示例
import SwiftUI
struct ProgressTest: View {
let timer = Timer.publish(every: 0.03, on: .main, in: .common).autoconnect()
@State var value:Double = 0.0
var body: some View {
List{
//无法定义颜色
ProgressView()
//无法隐藏Label
ProgressView("完成量", value: value, total: 100)
.accentColor(.red)
//自定义Style
ProgressView("工程进度",value: value, total: 100)
.progressViewStyle(MyProgressViewStyle())
}
.onReceive(timer) { _ in
if value < 100 {
value += 2
}
}
}
}
//定义方法都大同小异。
struct MyProgressViewStyle:ProgressViewStyle{
let foregroundColor:Color
let backgroundColor:Color
init(foregroundColor:Color = .blue,backgroundColor:Color = .orange){
self.foregroundColor = foregroundColor
self.backgroundColor = backgroundColor
}
func makeBody(configuration: Configuration) -> some View {
GeometryReader{ proxy in
ZStack(alignment:.topLeading){
backgroundColor
Rectangle()
.fill(foregroundColor)
.frame(width:proxy.size.width * CGFloat(configuration.fractionCompleted ?? 0.0))
}.clipShape(RoundedRectangle(cornerRadius: 10))
.overlay(
configuration.label
.foregroundColor(.white)
)
}
}
}
编辑于 2023-10-28 15:25・IP 属地未知 标签:foregroundColor,进度条,HowTo,value,backgroundColor,SwiftUI2.0,100,ProgressView From: https://www.cnblogs.com/sexintercourse/p/18546810