iOS的ProgressBar 13

布拉姆

我尝试创建自己的设备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 14版本和支持iOS 13的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实现失败。有人可以帮我吗?

他的脾气

进度是-NSObject,它不是结构,因此状态对其不起作用。您必须使用KVO来观察Progress中的更改,然后重定向到SwiftUI视图的真实来源。

这是可能解决方案的简化演示。经过Xcode 12.1 / iOS 14.1测试

演示

class ProgressBarViewModel: ObservableObject {
    @Published var fractionCompleted: Double
    let progress: Progress
    private var observer: NSKeyValueObservation!
    
    init(_ progress: Progress) {
        self.progress = progress
        self.fractionCompleted = progress.fractionCompleted
        observer = progress.observe(\.completedUnitCount) { [weak self] (sender, _) in
            self?.fractionCompleted = sender.fractionCompleted
        }
    }
}

struct ProgressBar: View {
    @ObservedObject private var vm: ProgressBarViewModel
    
    init(_ progress: Progress) {
        self.vm = ProgressBarViewModel(progress)
    }
    
    var body: some View {
        VStack(alignment: .leading) {
            Text("\(Int(vm.fractionCompleted * 100))% 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(vm.fractionCompleted))
                }
            }.frame(height: 4)
            Text("\(vm.progress.completedUnitCount) of \(vm.progress.totalUnitCount)")
                .font(.footnote)
                .foregroundColor(.gray)
        }
    }
}

和更新的呼叫位置以使用相同的构造函数

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)      // << here !!

// ... other code

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS 13替代“ setAnimationCurve”

来自分类Dev

iOS13系统UITabBar

来自分类Dev

iOS 13中的preferredsStatusBarHidden问题

来自分类Dev

iOS 13和静音通知

来自分类Dev

透明导航栏iOS 13

来自分类Dev

iOS 13 SwiftUI中的UIToolbar?

来自分类Dev

iOS-需要iOS 13+

来自分类Dev

自iOS 13起UIAlertController消失了

来自分类Dev

iOS 13 SearchController后退按钮颜色

来自分类Dev

在iOS 13中停止接收推送通知

来自分类Dev

iOS 13中的UITabBar透明标签错误

来自分类Dev

iOS 13中的SceneDelegate,是否有必要?

来自分类Dev

iOS 13辅助功能语音控制功能

来自分类Dev

崩溃:iOS 13中的Foundation NSClassFromString + 200

来自分类Dev

iOS 13深色模式重音颜色

来自分类Dev

在iOS 13中隐藏状态栏

来自分类Dev

WKWebView iOS 13中的DeviceMotion和DeviceOrientation

来自分类Dev

模拟iOS 13全屏对话框

来自分类Dev

iOS 13上的UITabBarController色彩颜色问题

来自分类Dev

IOS 13中的滚动问题

来自分类Dev

在sceneDidEnterBackground(iOS13)上的URL请求

来自分类Dev

iOS 13和14上的NSKeyValuePopPendingNotificationLocal Crash

来自分类Dev

更改UIITabbar宽度ios13快速

来自分类Dev

ios 13 Urban Airship 后台推送失败

来自分类Dev

Xamarin.iOS应用程序在iOS 13上冻结

来自分类Dev

VisionKit框架iOS 13中的iOS应用崩溃

来自分类Dev

试图使iOS 13项目与iOS 12兼容

来自分类Dev

从iOS 13 UIDatepicker到iOS 14 UIDatepicker的转换

来自分类常见问题

如何获得对iOS 13+中状态栏的引用?