【发布时间】:2021-01-18 17:53:05
【问题描述】:
我尝试创建自己的 ProgressView 来支持 iOS 13,但由于某种原因它似乎无法正常工作。我试过@State、@Binding 和普通的var progress: Progress,但它根本没有更新。
struct ProgressBar: View {
@Binding var progress: Progress
var body: some View {
VStack(alignment: .leading) {
Text("\(Int(progress.fractionCompleted))% completed")
ZStack {
RoundedRectangle(cornerRadius: 2)
.foregroundColor(Color(UIColor.systemGray5))
.frame(height: 4)
GeometryReader { metrics in
RoundedRectangle(cornerRadius: 2)
.foregroundColor(.blue)
.frame(width: metrics.size.width * CGFloat(progress.fractionCompleted))
}
}.frame(height: 4)
Text("\(progress.completedUnitCount) of \(progress.totalUnitCount)")
.font(.footnote)
.foregroundColor(.gray)
}
}
}
在我的内容视图中,我添加了 iOS 14 变体和支持 iOS 13 的变体。它们看起来一样,但 iOS 13 版本并没有改变任何东西。
struct ContentView: View {
@State private var progress = Progress(totalUnitCount: 10)
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
ProgressBar(progress: $progress)
.padding()
.onReceive(timer) { timer in
progress.completedUnitCount += 1
if progress.isFinished {
self.timer.upstream.connect().cancel()
progress.totalUnitCount = 500
}
}
if #available(iOS 14, *) {
ProgressView(progress)
.padding()
.onReceive(timer) { timer in
progress.completedUnitCount += 1
if progress.isFinished {
self.timer.upstream.connect().cancel()
progress.totalUnitCount = 500
}
}
}
}
}
}
iOS 14 变体有效,但我的 iOS 13 实现失败。有人可以帮帮我吗?
【问题讨论】:
标签: swift swiftui progress-bar combine