我有一个 NSProgressIndicator(进度条),当在这样的委托(delegate)方法中时,它似乎可以完美地工作:
func restClient(client: DBRestClient!, uploadProgress progress: CGFloat, forFile destPath: String!, from srcPath: String!) {
if progress == 1.0 {
progressBar.hidden = true
generalAlert("Uploaded", text: "Your File has been saved to your computer and uploaded under: \(destPath)")
} else {
progressBar.hidden = false
print(progress) // e.g 0.010213
progressBar.doubleValue = Double(progress)*100
}
}
但是,当放置在带有回调的函数中时,我似乎无法以任何方式修改它(既不接受我的 isHidden = false 调用,也不接受 doubleValue 更新)。
CloudConvert.convert([
"inputformat": "gif",
"outputformat" : "mov",
"input" : "upload",
"file": name],
progressHandler: { (step, percent, message) -> Void in
print(percentage!) //e.g 10.31451. Called every second
self.progressBar.hidden = false //Doesn't Work
self.progressBar.doubleValue = Double(percentage!) //Doesn't Work
},
completionHandler: { (path, error) -> Void in
if(error != nil) {
self.progressBar.hidden = true
self.generalAlert("ERROR", text: "Your file could not be uploaded")
} else {
self.progressBar.hidden = true
self.generalAlert("MOV Uploaded", text: "Your MOV has been saved to your computer and uploaded")
}
})
谁能解释为什么会这样?
最佳答案
您的完成处理程序将在后台线程中运行所有内容,您需要在主线程上进行 UI 调用。
像这样将进度条调用打包到主队列中
DispatchQueue.main.async(execute: {
self.progressBar.hidden = false //Should now Work
self.progressBar.doubleValue = Double(percentage!) //Should now Work
})
关于swift - NSProgressIndicator 无法在带有回调的函数内部工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41878239/