问题描述
我有一个 SwiftUI 项目,我在其中使用了 Apple 的按需资源 (ODR)。
我尝试过的:
以下代码请求 ODR 资源,将 NSBundleResourceRequest
存储在静态属性中,以便在其他地方可用:
class ondemandResources {
static var request: NSBundleResourceRequest?
func loadMusic() {
ondemandResources.request = NSBundleResourceRequest(tags: Set(["Music"]))
ondemandResources.request?.conditionallyBeginAccessingResources { resourcesAvailable in
dispatchQueue.main.async {
if resourcesAvailable {
//Resource is already on the device. No need to download.
} else {
//Resource needs to be downloaded.
ondemandResources.request?.beginAccessingResources { error in
dispatchQueue.main.async {
if error == nil {
//Success
}
}
}
}
}
}
}
}
然后我使用 UIViewRepresentable
连接 UIProgressView
,将视图的 observedProgress
属性设置为上述静态请求对象的 progress
属性:
struct ProgressBar: UIViewRepresentable {
func makeUIView(context: Context) -> UIProgressView {
let progressView = UIProgressView()
// Use the static NSBundleResourceRequest:
progressView.observedProgress = ondemandResources.request?.progress
return progressView
}
func updateUIView(_ uiView: UIProgressView,context: Context) {
}
}
根据 documentation,设置 observedProgress
属性应该会导致进度值自动更改:
设置此属性后,进度视图会使用从 Progress 对象接收到的信息自动更新其进度值。
最后,我在 SwiftUI 视图中使用我的 ProgressBar
,如下所示:
struct MyView: View {
var body: some View {
ProgressBar()
}
}
结果:
问题:
为什么我的 ProgressBar
不显示 ODR 下载进度?我究竟做错了什么?这是这样做的方法,还是有更简单的方法?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)