2017-01-16 44 views
0

我想顯示進度視圖的文件,我下載時,我打印的進度是正確的,但是當我在進度視圖上實現它是完全錯誤的,它會到3000這裏是我的代碼。我使用MHRadialProgressView鏈接UIProgressView顯示下載進度使用Firebase

downloadTask.observe(.progress) { (snapshot) -> Void in 
    // Download reported progress 

    let percentComplete = 100 * Double(snapshot.progress!.completedUnitCount)/Double(snapshot.progress!.totalUnitCount) 
    print("percentComplete") 
    self.progressView.moveNext(currentprogress as NSNumber!) 

    // Update the progress indicator 
} 

當我用MBProgressHUD這裏是它

downloadTask.observe(.progress) { (snapshot) -> Void in 
       // Download reported progress  
       let percentComplete = 100 * Float(snapshot.progress!.completedUnitCount) 
        /Float(snapshot.progress!.totalUnitCount) 

      let hud = MBProgressHUD.showAdded(to: (self.navigationController?.view)!, animated: true) 
      // Set the determinate mode to show task progress. 
      hud.mode = MBProgressHUDMode.determinate 

      hud.label.text = NSLocalizedString("Loading...", comment: "HUD loading title") 
      // Set up NSProgress 
      let progressObject = Progress(totalUnitCount: 100) 
      hud.progressObject = progressObject 
      // Configure a cancel button. 


      DispatchQueue.global(qos: .default).async(execute: {() -> Void in 
       // Do something useful in the background and update the HUD periodically. 

       while progressObject.fractionCompleted < 1.0 { 

        progressObject.becomeCurrent(withPendingUnitCount: 1) 
        progressObject.resignCurrent() 
        usleep(useconds_t(percentComplete)) 
       } 

       DispatchQueue.main.async(execute: {() -> Void in 
        hud.hide(animated: true) 
       }) 
      }) 
     } 

我收到此錯誤 nanfatal錯誤代碼:無法轉換爲UInt32的浮動值,因爲它要麼是無窮的, NaN的

回答

1

幾個建議:

  • 不要使用此庫(自2014年以來未更新),請使用MBProgressHUD,它允許您使用set a progress object and forget about it
  • 如果您仍然想使用原始庫,請務必閱讀文檔 - 它說,「或無序progres [原文如此(通常與用戶操作相關的步驟),您可以通過以下方式增加進度:值」。這意味着您需要將當前的進度與以前的進度進行比較,並自行增加或減少進度。
+0

請檢查上面的代碼,當我使用MBProgressHUD我得到這個錯誤。 nanfatal錯誤:浮點值無法轉換爲UInt32,因爲它是無限的或NaN – Khallad

+1

你能解釋爲什麼你不只是這樣做:'hud.progressObject = snapshot.progress'?其他一切看起來都很複雜且沒有必要。 –