2014-07-10 22 views
1

用户可以通过单击按钮来下载一组地图,完成该任务后,我想隐藏进度指示器。我已经尝试了以下代码的几种变体,但尚未实现我正在寻找的功能。任何指导将不胜感激。MBProgressHUD GCD之后的隐藏指示器

- (IBAction)SavePhotoOnClick:(id)sender{ 


    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; 
    [self.navigationController.view addSubview:HUD]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
     NSURL *url = [NSURL URLWithString:urlstr1]; 
     UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);  dispatch_async(dispatch_get_main_queue(), ^{ 
      [MBProgressHUD hideHUDForView:self.view animated:YES]; 
     }); 
    }); 



     // Set determinate mode 
    HUD.mode = MBProgressHUDModeDeterminate; 
    HUD.dimBackground = YES; 
    HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90]; 
    HUD.delegate = self; 
    HUD.labelText = @"Downloading Maps"; 

    // myProgressTask uses the HUD instance to update progress 
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES]; 
    } 


- (void)myProgressTask { 
    // This just increases the progress indicator in a loop 
    float progress = 0.0f; 
    while (progress < 1.0f) { 
     progress += 0.01f; 
     HUD.progress = progress; 
     usleep(40000); 
    } 
} 

删除progresstask方法和showWhileExecuting代码将删除进度指示器。看起来,使用者会覆盖所有内容,并且不允许在下载完成后隐藏进度指示器,而是在4秒后将其删除。

回答

1

你的问题是,myProgressTask不执行任何实际工作:你的下载实际上发生在你的dispatch_async调用中。在开始网络请求之前,请使用类似[hud showHUDAddedTo: self.navigationController.view animated:YES];的呼叫,以便在下载完成后隐藏。这只会显示旋转指示符,而不是进度循环。

如果您需要进度圈,则需要使用NSURLConnection及其关联的delegate methods来跟踪下载进度。具体来说,查看connection:didReceiveResponse:方法 - 使用expectedContentLength来计算百分比,使用从connection:didReceiveData:方法获得的值逐步更新此进度。

查看this question了解如何操作的示例。

+0

这个工作,我以前做过。出于某种原因,但是当我使用该调用时,它不允许我添加文本,颜色等。我对进度指示器的确定状态没有太大的限制,假设我可以添加一些文字说“正在下载.. “任何protips? – 3722839

+0

我相信MBProgressHUD实例具有可以设置为自定义外观的“文本”参数。 –

+0

是的,但是,这些参数似乎不适用于我们目前的解决方案。我需要进一步调查它,我想。 – 3722839