【问题标题】:MBProgressHUD Hiding indicator after GCDMBProgressHUD GCD 后隐藏指示器
【发布时间】:2014-07-10 00:16:29
【问题描述】:

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

 - (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 代码会删除进度指示器。似乎 usleep 覆盖了所有内容,并且不允许在下载完成后隐藏进度指示器,而是在 4 秒后将其删除。

【问题讨论】:

    标签: ios objective-c grand-central-dispatch mbprogresshud


    【解决方案1】:

    您的问题是 myProgressTask 没有执行任何实际工作:您的下载实际上发生在您的 dispatch_async 调用中。在你开始你的网络请求之前使用像[hud showHUDAddedTo: self.navigationController.view animated:YES];这样的电话,所以当你的下载完成时它会被隐藏。这只会显示旋转指示器,而不是进度圈。

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

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

    【讨论】:

    • 这行得通,我以前做过。但是由于某种原因,当我使用该调用时,它不允许我添加文本、颜色等。我对进度指示器的确定状态不是那么大,假设我可以添加一些文本说“正在下载.. ." 有什么建议吗?
    • 我相信 MBProgressHUD 实例具有诸如“文本”之类的参数,您可以设置这些参数来自定义外观。
    • 是的,但是,这些参数似乎不适用于我们当前的解决方案。我想我需要再调查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多