为什么alamofire下载警报的进度会闪烁,后面的屏幕开始变黑?

在使用alamofire下载时,我有警告的进度视图,但是问题是,当我想要下载alert时的进度视图将会开始完成,但闪烁,另一个问题是后面的屏幕将开始淡化为黑色每秒钟都会有一个新的提示,进度视图会显示,最后有很多提醒,我怎样才能避免它们?

downloadTimer = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(flashingDownload), userInfo: nil, repeats: true) let destination = DownloadRequest.suggestedDownloadDestination() Alamofire.download("http://example.com", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in print("Progress: \(progress.fractionCompleted)") sixthLevelViewController.progressdownload = Float(progress.fractionCompleted) if sixthLevelViewController.progressdownload == 1.0 { self.downloadfinished = true let alertController = UIAlertController(title: "finish download ", message: " download Completed! ", preferredStyle: UIAlertControllerStyle.alert) let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in self.view.isUserInteractionEnabled = true } alertController.addAction(okAction) self.present(alertController, animated: true, completion: nil) alertController.view.tintColor = UIColor.green alertController.view.backgroundColor = UIColor.green alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width } } .validate().responseData { ( response ) in print(response.destinationURL!.lastPathComponent) } 

这里是定时器的代码

  func flashingDownload() { while (topViewController.presentedViewController != nil){ topViewController = topViewController.presentedViewController! } DispatchQueue.main.async { let alert = UIAlertController(title: "downloading", message: "pls wait", preferredStyle: .alert) let progressBar = UIProgressView(progressViewStyle: .default) progressBar.setProgress(sixthLevelViewController.progressdownload, animated: true) progressBar.frame = CGRect(x: 10, y: 70, width: 250, height: 0) alert.view.addSubview(progressBar) self.topViewController.present(alert, animated: false, completion: nil) if sixthLevelViewController.progressdownload == 1.0 { print("finished download !") self.topViewController.dismiss(animated: false , completion: nil) self.downloadTimer.invalidate() } } 

正如你在我的代码中看到的,我有另一个控制下载完成的variables,所以当进度是1.0时,我想closures警报

这里的问题是,你正在'downloadProgress'闭包中呈现alert视图。 这不是它的意思。

每次进程发生变化时,都会调用“downloadProgress”闭包。 所以当下载一个文件的时候,显然这可能会导致许多警报视图层叠在一起,正如你所描述的那样。

其实你应该在外面的地方实例化你的Alert View,并且只使用这个闭包来更新它的内容。

但是,如果你想保持这个结构,testingprogress.isFinished而不是一个特定的值。