2017-09-10 3 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() 

      } 

     } 

ためのコードあなたは私のコードに見るように、私は進歩が、私は警告

答えて

0

を却下したい1.0であるときに、ダウンロードフィニッシュを制御し、別の変数を持っていますここで問題となるのは、「downloadProgress」クロージャの中にアラートビューを表示していることです。これはそれが意味するものではありません。

'downloadProgress'クロージャは、基本的に進行状況が変更されたときに呼び出されます。 ファイルをダウンロードすると、明らかに、説明したように複数のアラートビューが重なり合って表示される可能性があります。

実際には、アラートビューを外側にインスタンス化し、このクロージャを使用して内容を更新する必要があります。

ただし、この構造を保持する場合は、特定の値の代わりにprogress.isFinishedをテストしてください。

関連する問題