我敢打赌这是一个非常基本的问题,但是我无法让它发挥作用。我正在从AWS S3下载图像,能够获得下载的进度,并能够相应地更新进度变量。但是,我找不到基于此变量为进度条设置动画的方法。
(诚然笨重)代码如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.refresh() //re-gets user info. Having trouble with AWS randomly claiming I'm attempting to download using UnAuth even though I am logged in
tableView.deselectRow(at: indexPath, animated: true)
self.downloadProgressView.isHidden = false
self.proprietaryObject = proprietaryObjects[indexPath.row]
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
self.progress = progress
})
}
let transferUtility = AWSS3TransferUtility.default()
let resource = "\(self.proprietaryObject?.value1 ?? "NA")_\(self.proprietaryObject?.value2 ?? "NA")_\(self.proprietaryObject?.firstName ?? "NA")_\(self.proprietaryObject?.lastName ?? "NA")"
let type = "jpeg"
self.downloadingFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resource).\(type)")
transferUtility.download(to: self.downloadingFileURL!, bucket: AWSS3BucketName, key: "\(resource).\(type)", expression: expression, completionHandler: { (task, URL, Data, Error) in
if Error != nil {
print (Error!)
}
if let data = Data {
print("Data: \(data)")
let image = UIImage(data: data)
self.downloadedImage = image
}
}).continueWith(executor: AWSExecutor.default()) { (task) -> Any? in
if let error = task.error {
print("download error: \(error)")
self.refresh()
}else {
print("Download started")
print("User: \(self.user?.username ?? "NO USER")")
self.animatePath()
}
return nil
}
}
func animatePath() {
print("Animation Started")
if !(self.view.subviews.contains(downloadProgressView)) {
self.view.addSubview(downloadProgressView)
downloadProgressView.center = self.view.center
self.downloadProgressView.isHidden = false
self.downloadProgressView.layer.addSublayer(self.shapeLayer)
}else {
self.view.bringSubview(toFront: self.downloadProgressView)
self.downloadProgressView.isHidden = false
downloadProgressView.center = self.view.center
if !((downloadProgressView.layer.sublayers?.contains(self.shapeLayer))!) {
self.downloadProgressView.layer.addSublayer(self.shapeLayer)
}
}
func animate() {
if self.progress?.fractionCompleted == nil {
self.fraction = 0
}else {
self.fraction = 0 + (self.progress?.fractionCompleted)!
}
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = CGFloat(self.shapeLayer.strokeEnd)
animation.toValue = CGFloat(self.fraction!)
animation.duration = 0.2
animation.fillMode = kCAFillModeForwards
self.shapeLayer.strokeEnd = CGFloat(self.fraction!)
self.shapeLayer.add(animation, forKey: "animation")
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1) , execute: {
//the one second delay gives self.progress.fractionCompleted time to be non-nil
repeat {
animate()
print("Completed: \((self.progress?.fractionCompleted)! * 100)%")
} while self.fraction! < 1
if self.fraction == 1 {
print("fraction achieved 100%. resetting to 0%")
self.fraction = 0
self.progress = nil
self.performSegue(withIdentifier: "proprietarySegue", sender: self)
}
})
}
控制台记录递增的&#34; .fractionCompleted&#34; %就好了,当它达到100%时,它确实执行了segue。以下ViewController的UIImageView中的图像确实是下载的图像(覆盖为未显示的segue做准备)。但我只获得了进步圈的闪光。通常,它显示没有进展然后执行segue。紧接着第一个segue之后,对于每次后续下载,downloadProgressView都不会出现或显示100%完成,然后才会消失。
我有一种感觉,我做了一些根本错误的事情和/或它与线程有关......?
任何帮助都将不胜感激。
答案 0 :(得分:0)
self.animatePath()
的完成块中调用 transferUtility
。这就是为什么动画将在下载结束时发生并且segue将会执行。
尝试:
let expression = AWSS3TransferUtilityDownloadExpression()
expression.progressBlock = {(task, progress) in
DispatchQueue.main.async(execute: {
self.progress = progress
self.animatePath()
})
}