2016-08-21 203 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