新博客:
MBProgressHUD是一個開源類庫,實現了各種樣式的提示框, 下載地址:https://github.com/jdg/MBProgressHUD,然后把兩個MBProgressHUD.h和MBProgressHUD.m放到自己的項目就可以了。這里有一個小Demo可以參考一下。
頭文件部分:
#import <UIKit/UIKit.h> #import "MBProgressHUD.h" @interface ViewController : UIViewController { MBProgressHUD *HUD; } - (IBAction)showTextDialog:(id)sender; //文本提示框,默認情況下 - (IBAction)showProgressOne:(id)sender; //第一種加載提示框 - (IBAction)showProgressTwo:(id)sender; //第二種加載提示框 - (IBAction)showProgressThree:(id)sender; //第三種加載提示框 - (IBAction)showCustomDialog:(id)sender; //自定義提示框,顯示打鈎效果 - (IBAction)showAllTextDialog:(id)sender; //顯示純文本提示框 @end
實現文件部分
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)showTextDialog:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.dimBackground = YES; //把當前的view置於后台 HUD.labelText = @"請稍等"; //顯示對話框 [HUD showAnimated:YES whileExecutingBlock:^{ sleep(3); } completionBlock:^{ }]; } - (IBAction)showProgressOne:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"正在加載"; HUD.mode = MBProgressHUDModeDeterminate; //HUD.mode = MBProgressHUDModeAnnularDeterminate; [HUD showAnimated:YES whileExecutingBlock:^{ float progress = 0.0f; while (progress < 1.0f) { progress += 0.01f; HUD.progress = progress; usleep(5000); } } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (IBAction)showProgressTwo:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"正在加載"; HUD.mode = HUD.mode = MBProgressHUDModeAnnularDeterminate; [HUD showAnimated:YES whileExecutingBlock:^{ float progress = 0.0f; while (progress < 1.0f) { progress += 0.01f; HUD.progress = progress; usleep(5000); } } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (IBAction)showProgressThree:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; HUD.labelText = @"正在加載"; HUD.mode = MBProgressHUDModeDeterminateHorizontalBar; [HUD showAnimated:YES whileExecutingBlock:^{ float progress = 0.0f; while (progress < 1.0f) { progress += 0.01f; HUD.progress = progress; usleep(5000); } } 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; HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease]; [HUD showAnimated:YES whileExecutingBlock:^{ sleep(2); } completionBlock:^{ [HUD removeFromSuperview]; [HUD release]; HUD = nil; }]; } - (void)dealloc { [super dealloc]; } @end
實現效果如圖所示:
1、默認效果,也就是MBProgressHUDModeIndeterminate
2、第一種加載提示框,MBProgressHUDModeDeterminate
3、第二種加載提示MBProgressHUDModeAnnularDeterminate
4、第三種加載提示框,MBProgressHUDModeDeterminateHorizontalBar
5、自定義提示框 ,可以帶圖片的MBProgressHUDModeCustomView
6.純文本提示框
如果有什么問題,歡迎通過微博交流 @Linux_小木頭