MBProgressHUD是一個開源項目,實現了不少種樣式的提示框,使用上簡單、方便,而且能夠對顯示的內容進行自定義,功能很強大,不少項目中都有使用到。到GitHub上能夠下載到項目源碼https://github.com/jdg/MBProgressHUD,下載下來後直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,別忘了選擇拷貝到工程。完了在須要使用的地方導入頭文件就能夠開始使用了。首先看下工程截圖:git
接下來是整個Demo的完整界面,這裏我只選擇出了幾個經常使用的對話框,其餘樣式的在源碼提供的Demo裏能夠找到,要用的話直接參考就能夠。github
接下來直接上代碼了,頭文件部分:微信
#import <UIKit/UIKit.h> #import "MBProgressHUD.h" @interface ViewController : UIViewController { //HUD(Head-Up Display,意思是擡頭顯示的意思) MBProgressHUD *HUD; } - (IBAction)showTextDialog:(id)sender; - (IBAction)showProgressDialog:(id)sender; - (IBAction)showProgressDialog2:(id)sender; - (IBAction)showCustomDialog:(id)sender; - (IBAction)showAllTextDialog:(id)sender; @end
實現文件(按鈕實現部分):spa
- (IBAction)showTextDialog:(id)sender { //初始化進度框,置於當前的View當中 HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; //若是設置此屬性則當前的view置於後臺 HUD.dimBackground = YES; //設置對話框文字 HUD.labelText = @"請稍等"; //顯示對話框 [HUD showAnimated:YES whileExecutingBlock:^{ //對話框顯示時須要執行的操做 sleep(3); } completionBlock:^{ //操做執行完後取消對話框 [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (IBAction)showProgressDialog:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"正在加載"; //設置模式爲進度框形的 HUD.mode = MBProgressHUDModeDeterminate; [HUD showAnimated:YES whileExecutingBlock:^{ float progress = 0.0f; while (progress < 1.0f) { progress += 0.01f; HUD.progress = progress; usleep(50000); } } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (IBAction)showProgressDialog2:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"正在加載"; HUD.mode = MBProgressHUDModeAnnularDeterminate; [HUD showAnimated:YES whileExecutingBlock:^{ float progress = 0.0f; while (progress < 1.0f) { progress += 0.01f; HUD.progress = progress; usleep(50000); } } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (IBAction)showCustomDialog:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"操做成功"; HUD.mode = MBProgressHUDModeCustomView; HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease]; [HUD showAnimated:YES whileExecutingBlock:^{ sleep(2); } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (IBAction)showAllTextDialog:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"操做成功"; HUD.mode = MBProgressHUDModeText; //指定距離中心點的X軸和Y軸的偏移量,若是不指定則在屏幕中間顯示 // HUD.yOffset = 150.0f; // HUD.xOffset = 100.0f; [HUD showAnimated:YES whileExecutingBlock:^{ sleep(2); } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; }
依次實現的效果以下:.net
下面這個效果就相似Android中的Toast:code
以上就簡單介紹了MBProgressHUD的使用,這裏都是採用block的形式來操做的,這樣寫起代碼來更直觀也更高效。orm
加入咱們的QQ羣或微信公衆帳號請查看:Ryan's zone公衆帳號及QQ羣blog
歡迎關注個人新浪微博和我交流:@唐韌_Ryanrem