2017-09-10 88 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而不是为特定的值。