SwiftUI 简明教程之指示器

ProgressView
ProgressView 有两种呈现形式,一种是菊花样式,另一种是进度条样式,二者分别对应 ProgressViewStyle 中的 CircularProgressViewStyle 和 LinearProgressViewStyle。

如果我们没有通过绑定的浮点类型的值动态更新 ProgressView 的 value 值,那么它的默认样式就是 CircularProgressViewStyle,即菊花样式。比如:

ProgressView()
或者:

ProgressView(“加载中…”)
我们还可以通过 .foregroundColor(.blue) 改变文字的颜色,如果我们要修改菊花的颜色,那么可以这样指定:.progressViewStyle(CircularProgressViewStyle(tint: .orange)) 。

而要修改进度条的颜色,则可以通过 .accentColor(.orange) 实现。

进度条样式的实现也比较简单,当然我们也可以自定义 ProgressViewStyle。

如下是示例所示的全部代码,供参考:

struct IndicatorsView: View {
@State private var progress = 0.0 {
didSet {
if progress == 100 {
title = “下载完成!”
systemImgName = “checkmark.seal.fill”
} else {
title = “下载ing…”
systemImgName = “square.and.arrow.down”
}
}
}
@State private var title = “下载ing…”
@State private var systemImgName = “square.and.arrow.down”
/// 定时器
private let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()

var body: some View {
List {
// SectionHeaderView 是笔者自定义的视图控件
Section(header: SectionHeaderView(chapter: “ProgressView”, section: nil)) {
ProgressView()
.centerHorizontal() // .centerHorizontal() 是笔者自定义的修饰器

    ProgressView("加载中...")
      .progressViewStyle(CircularProgressViewStyle(tint: .orange))
      .centerHorizontal()
      .footnote(".progressViewStyle(CircularProgressViewStyle(tint: .orange))")
    
    ProgressView(title, value: progress, total: 100)
      .foregroundColor(.blue)
      .accentColor(.orange)
      .footnote(".foregroundColor(.blue)\n.accentColor(.orange)")
    
    ProgressView(value: progress, total: 100) {
      HStack {
        Image(systemName: systemImgName)
        Text(title)
      }
      .foregroundColor(.blue)
    } currentValueLabel: {
      Text("\(Int(progress))%").foregroundColor(.orange)
    }
    .footnote("自定义视图")
  }
  .onReceive(timer) { _ in // 接收定时器更新事件
    if progress < 100 {
      progress = min(100, progress + Double(arc4random_uniform(5)+1))
    }
  }
}
.listStyle(InsetGroupedListStyle())

}
}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值