SwiftUI 简明教程之指示器
2021-05-11 20:28
886 查看
本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容。
Eul 是一款 SwiftUI & Combine 教程 App(iOS、macOS),以文章(文字、图片、代码)配合真机示例(Xcode 12+、iOS 14+,macOS 11+)的形式呈现给读者。笔者意在尽可能使用简洁明了的语言阐述 SwiftUI & Combine 相关的知识,使读者能快速掌握并在 iOS 开发中实践。
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()) } }
本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容。
相关文章推荐
- Lua脚本语言简明入门教程
- Android OpenGL ES 简明开发教程 02 <构造OpenGL ES View>
- 机器学习之k近邻算法——3、matplotlib简明教程
- PHP微信发红包简明教程
- Git简明教程
- ASP.NET AJAX简明教程
- J2ME简明教程( 第六章)
- 如何搭建一个独立博客——简明Github Pages与Hexo教程
- Linux环境编程简明教程(1)获取进程相关信息的操作
- Eclipse团队CVS简明教程
- ExtJS2.0实用简明教程
- Appium简明教程
- CocoaPods使用简明教程
- ANT简明教程[转载]
- Java23个设计模式的简明教程
- 简明 Python 教程-win10下备份脚本
- Hadoop 简明教程
- CentOs6.5中安装和配置vsftp简明教程
- C语言简明教程--指针1
- 黑客教程系列-简明批处理