iOS 進度條的實現
在iOS的開發當中,經常會遇到讀取系統資源等類似的情況,如果網絡比較卡的話,用戶很可能以為這個app已經掛掉了,用戶體驗很差,老外還是很好的,提供開源的source,跟大家一塊學習下。
iOS的進度條可以分為幾類,有普通的,就像一個圈圈在那轉,有在圈圈下加文字的,有直接是純文字的,等等。。
在自己的項目中需要加入以下2個文件:MBProgressHUD.h和MBProgressHUD.m;接下來我們只需要在我們的.m文件中引用progress的頭文件即可。
對于普通的進度條,代碼如下:
- (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中可以做我們想要做的事情:
- (void)myTask { // Do something usefull in here instead of sleeping ... sleep(3); }
效果如圖:
帶有文字的進度條:
- (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]; }效果圖如下:
純文字的代碼:
- (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]; }
效果圖如下:
參考地址:https://github.com/jdg/MBProgressHUD
來自:http://blog.csdn.net/weasleyqi/article/details/8072897
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!