2017-09-10 47 views
1

我有戒備發展觀與alamofire下載時,但問題是,當我想下載警報進度視圖將開始完成,但閃爍,另一個問題是,後面的屏幕將開始成爲褪色黑色我認爲每秒都會顯示一個帶有進度視圖的新警報,並且最後會有很多警報,我該如何避免它們?爲什麼alamofire下載警報的進度會閃爍,後面的屏幕開始變黑?

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

      } 

     } 

代碼正如你在我的代碼看,我認爲控制下載完成另一個變量,所以當進度爲1.0我想關閉提醒

回答

0

這裏的問題是,你正在'downloadProgress'閉包內呈現alert視圖。這不是它的意思。

的「downloadProgress」封閉基本上被調用每次有一直在進步的變化的時間。 所以下載文件時,這顯然會導致像你描述的分層在彼此的頂部許多警報意見。

其實你應該實例化警報視圖之外的某處只有用這種封閉來更新它的內容。

但是,如果你想保持這種結構,測試progress.isFinished而不是爲特定的值。