在iOS的開發當中,經常會遇到讀取系統資源等類似的情況,如果網路比較卡的話,使用者很可能以為這個app已經掛掉了,使用者體驗很差,老外還是很好的,提供開源的source,跟大家一塊學習下。
iOS的進度條可以分為幾類,有普通的,就像一個圈圈在那轉,有在圈圈下加文字的,有直接是純文字的,等等。。
在自己的項目中需要加入以下2個檔案:MBProgressHUD.h和MBProgressHUD.m;接下來我們只需要在我們的.m檔案中引用progress的標頭檔即可。
對於普通的進度條,代碼如下:
[plain]
- (IBAction)showSimple:(id)sender {
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
// Regiser for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}
選取器函數myTask中可以做我們想要做的事情:
[plain]
- (void)myTask {
// Do something usefull in here instead of sleeping ...
sleep(3);
}
效果
帶有文字的進度條:
[plain]
- (IBAction)showWithLabel:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}
如下:
純文字的代碼:
[plain]
- (IBAction)showTextOnly:(id)sender {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
// Configure for text only and offset down
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Some message...";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
}
如下: