ios - 还押资源 - 预计时间(以及如何根据下载进度显示提醒)

标签 ios objective-c uialertview duration on-demand-resources

有没有办法获取点播资源下载的预计时间?

我想在它们全部下载之前显示一个提醒。

[alertDownload showCustom:self image:[UIImage imageNamed:@"icon.jpg"] 
                               color:[UIColor blueColor] 
                               title:@"Download..." 
                               subTitle:@"Download in progress" 
                               closeButtonTitle:nil 
                               duration: ODR ETA];

现在我有

if (request1.progress.fractionCompleted < 1) {
 // code above
}

但下载完成后警报不会自动消失,它会查看警报的持续时间

最佳答案

好吧,如果你能得到fraction complete值并且你能测量时间,那么你就知道你还剩下多长时间了。

开始下载时,在实例变量中记录开始时间:

@interface MyClass () {
    NSTimeInterval _downloadStartTime;
}

- (void)startDownload
{
    ...
    _downloadStartTime = [NSDate timeIntervalSinceReferenceDate];
    ...
}

然后在您的通知处理程序中,在您收到fraction complete 的地方,使用:

double fractionComplete = 0.2;    // For example
if (fractionComplete > 0.0) {     // Avoid divide-by-zero
    NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval elapsed = now - _downloadStartTime;
    double timeLeft = (elapsedTime / fractionComplete) * (1.0 - fractionComplete);
}

注意:我没有解决您显示警报对话框的问题,而且我认为您使用的逻辑不会起作用(您不希望每次获得更新时都显示新警报)。我避开了这整个区域,只专注于 ETA 逻辑。

关于ios - 还押资源 - 预计时间(以及如何根据下载进度显示提醒),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187416/

相关文章:

ios - 如何将 NSMutableArray 中的所有变量设置为零?

iphone - UIAlertView 在未记录的方法上崩溃

ios - 表格 View - 删除项目时清空背景空间

ios - glDrawElements 在 iOS 上大量使用 CPU

ios - 如何将自定义 View 添加到属性字符串或 TextView ?

ios - 由于未捕获的异常而终止应用程序 Swift

ios - 通过使用带有 ids 的排序 NSArray 来加速 for 循环

ios - 为什么我使用 NSPredicate 在数组中过滤的值会返回重复的输出?

iphone - 在 App 内购买 AlertMessage

objective-c - 如何为 UIAlertView 中的按钮编写事件处理程序?