为什么警报中的alamofire下载进度会闪烁并且背后的屏幕开始

为什么警报中的alamofire下载进度会闪烁并且背后的屏幕开始

本文介绍了为什么警报中的alamofire下载进度会闪烁并且背后的屏幕开始变黑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用alamofire下载时,我在警报中有进度视图,但是问题是当我要下载警报中的进度视图时将开始完成但闪烁,另一个问题是背后的屏幕将开始褪色变黑,我认为每秒钟都会显示一个带有进度视图的新警报,最后有很多警报我该如何避免?

I have progress view in alert when downloading with alamofire, but the problem is that when I want to download the progress view in alert will start to be complete but flashing and the other problem is that the behind screen will start to be fade to black I think in every second a new alert with progress view will show and at the end there are lots of alerts how can I avoid them?

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()

            }

       }

我的代码中我还有另一个变量来控制d ownload完成,所以当进度为1.0时,我想关闭警报

as you see in my codes I have another variable that controls the download finish so when progress is 1.0 I want to dismiss alert

推荐答案

这里的问题是,您正在显示警报视图 downloadProgress关闭。

The Problem here is that you are presenting the alert view inside the 'downloadProgress' closure. This is not what its meant for.

基本上,每次进度发生变化时,都会调用 downloadProgress关闭。
因此,下载文件时,这显然会导致许多警报视图如您所描述的那样彼此叠加。

The 'downloadProgress' closure basically gets called every time there has been a change in the progress.So when downloading a file this obviously can lead to many alert views layered on top of each other as you described.

实际上,您应该在某个地方实例化警报视图

Actually you should instantiate your Alert View somewhere outside and only use this closure to update it's content.

但是如果您想保留此结构,请测试 progress.isFinished 代替特定值。

But if you want to keep this structure, test for progress.isFinished instead for a specific value.

这篇关于为什么警报中的alamofire下载进度会闪烁并且背后的屏幕开始变黑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:19