2014-07-10 23 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