2016-08-21 246 views
2

我使用代碼顯示將文件從1%下載到100%的進度。在標籤中顯示百分比

if(downloadTask == _downloadTask26){ 


     _progress26 = [[NSNumber numberWithInteger:totalBytesWritten] floatValue]; 
     _total26 = [[NSNumber numberWithInteger:totalBytesExpectedToWrite] floatValue]; 

    } 

    if(downloadTask == _downloadTask27){ 


     _progress27 = [[NSNumber numberWithInteger:totalBytesWritten] floatValue]; 
     _total27 = [[NSNumber numberWithInteger:totalBytesExpectedToWrite] floatValue]; 

    } 

     float progress = _progress26 + _progress27; 
     float total = _total26 + _total27; 

     NSString *percentage = [NSString stringWithFormat:@"%.f%%", ((progress/total) * 100)]; 
     (NSLog (percentage, @"%.f%%")); 
     if (!_label3) { 

       _label3 = [[UILabel alloc] initWithFrame:CGRectMake(200.43, 158.84, 42, 19)]; 

       _label3.numberOfLines = 1; 
       _label3.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; 
       _label3.adjustsFontSizeToFitWidth = YES; 
       _label3.minimumScaleFactor = 10.0f/12.0f; 
       _label3.clipsToBounds = YES; 
       _label3.backgroundColor = [UIColor clearColor]; 
       _label3.textColor = [UIColor whiteColor]; 
       _label3.textAlignment = NSTextAlignmentCenter; 

       [_scroller addSubview:_label3]; 
      } 
     } 

     _label3.text = percentage; 
     if ([_label3.text isEqual: @"100%"]) { 
     } 

但是,當文件下載時,百分比不是以升序顯示。百分比以不同的順序顯示,如下面的視頻所示。我如何解決它?

視頻 https://drive.google.com/file/d/0B0EJbcHq3ZALUVRzanJ6SndscWc/view

回答

0

可能是由於下載該檢查是downloadingTask26或27個放日誌這兩個條件內,校驗下載和總規模任務條件。總任務可能會發生變化。

0

您是否簡化了您的問題中的代碼,其中生成視頻的代碼包含的內容僅包含下載26和27?只有兩次下載,我希望它只下降一次,但如果你有很多下載,我希望它每次下載開始時下降。底線,如果你真的有很多下載,結果是非常有意義的,因爲很可能你沒有多少下載的字節值,直到他們真正開始下載,然後下載開始,突然下載的字節總數將增加(因此百分比將下降)。

一個解決方案是使用NSProgress,爲每次下載創建一個子頁面NSProgress,然後更新父進程的百分比。這樣,尚未開始的下載將反映在總百分比中。

+0

是的,我簡化了代碼,因爲它非常大。你能展示如何在我的代碼中正確添加NSProgress嗎? – wefagaefesfsefsef

+0

查看優秀的WWDC 2015視頻[最佳進度報告實踐](https://developer.apple.com/videos/play/wwdc2015/232/)。 – Rob