MBProgressHUD第三方库使用

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

关键操作:

效果如下:

ViewController.h

1#import<UIKit/UIKit.h>2#import"MBPRogressHUD.h"34@interfaceViewController : UITableViewController<MBProgressHUDDelegate>5@property (strong, nonatomic) MBProgressHUD *hud;6@property (copy, nonatomic) NSArray *arrMode;7@property (copy, nonatomic) NSArray *arrModeName;89@end

ViewController.m

1#import"ViewController.h"23@interfaceViewController ()4- (void)loadData;5- (void)layoutUI;6- (void)taskOfIndeterminate;7- (void)taskOfDeterminate;8- (void)taskOfDeterminateHorizontalBar;9- (void)taskOfAnnularDeterminate;10- (void)taskOfCustomView;11- (void)taskOfText;12- (void)showHUDByIndeterminate;13- (void)showHUDByDeterminate;14- (void)showHUDByDeterminateHorizontalBar;15- (void)showHUDByAnnularDeterminate;16- (void)showHUDByCustomView;17- (void)showHUDByText;18@end1920@implementationViewController2122- (void)viewDidLoad {23[super viewDidLoad];2425[self loadData];26[self layoutUI];27}2829- (void)didReceiveMemoryWarning {30[super didReceiveMemoryWarning];31//Dispose of any resources that can be recreated.32}3334- (void)loadData {35_arrMode = @[@"MBProgressHUDModeIndeterminate",36@"MBProgressHUDModeDeterminate",37@"MBProgressHUDModeDeterminateHorizontalBar",38@"MBProgressHUDModeAnnularDeterminate",39@"MBProgressHUDModeCustomView",40@"MBProgressHUDModeText"];4142_arrModeName = @[@"UIActivityIndicatorView 来显示进度,这是默认值",43@"圆形饼图来显示进度",44@"水平进度条来显示进度",45@"圆环来显示进度",46@"自定义视图;例如通过这种方式,来显示一个正确或错误的提示图",47@"只显示文本"];48}4950- (void)layoutUI {51self.navigationItem.title =@"MBProgressHUD 第三方库使用";52}5354#pragmamark - MBProgressHUD Additional Task55- (void)taskOfIndeterminate {56sleep(3);//进程挂起3秒,这里仅仅是模拟,相当于执行了一些操作耗时3秒;sleep 和 usleep 都是进程挂起操作方法,他们的精准度不同,所以按需使用;对于一般秒级别操作,就使用 sleep 方法57}5859- (void)taskOfDeterminate {60CGFloat progressVal =0.0f;61while(progressVal <1.0) {62progressVal +=0.1;63_hud.progress =progressVal;64usleep(500000);//千分之一毫秒,即百万分之一秒;这里设置进程挂起0.5秒65}66}6768- (void)taskOfDeterminateHorizontalBar {69[self taskOfDeterminate];70}7172- (void)taskOfAnnularDeterminate {73[self taskOfDeterminate];74}7576- (void)taskOfCustomView {77[self taskOfIndeterminate];78}7980- (void)taskOfText {81[self taskOfIndeterminate];82}8384#pragmamark - MBProgressHUD85- (void)showHUDByIndeterminate {86UIColor *color =[UIColor cyanColor];8788_hud =[[MBProgressHUD alloc] initWithView:self.view];89_hud.activityIndicatorColor = color;//设置指示器颜色;默认为白色9091//label 和 detailsLabel 是公共部分,其他模式的展示效果一样可以用92_hud.labelText =@"加载中...";93_hud.labelFont = [UIFont systemFontOfSize:17];94_hud.labelColor = color;//设置文本颜色;默认为白色95_hud.detailsLabelText =@"用户请稍候,耐心等待";96_hud.detailsLabelFont = [UIFont systemFontOfSize:14];97_hud.detailsLabelColor = color;//设置详细文本颜色;默认为白色9899//一些额外不常用的设置100_hud.minShowTime =5.0f;101_hud.opacity =0.5f;102_hud.animationType =MBProgressHUDAnimationZoomOut;103_hud.cornerRadius =15.0f;104_hud.dimBackground =YES;105_hud.xOffset =0.0f;106_hud.yOffset =50.0f;107_hud.margin =30.0f;108_hud.square =YES;109_hud.minSize = CGSizeMake(240.0f,200.0f);//设置了 minSize 后,square 就失效了110111//设置委托,以便调用hudWasHidden:方法112_hud.delegate=self;113[self.view addSubview:_hud];114115//操作方式一:116//[_hud showWhileExecuting:@selector(taskOfIndeterminate)117//onTarget:self118//withObject:nil119//animated:YES];120121//操作方式二:122[_hud showAnimated:YES123whileExecutingBlock:^{124[self taskOfIndeterminate];125} completionBlock:^{126NSLog(@"showHUDByIndeterminate 执行完成");127}];128}129130- (void)showHUDByDeterminate {131_hud =[[MBProgressHUD alloc] initWithView:self.view];132_hud.mode =MBProgressHUDModeDeterminate;133[self.view addSubview:_hud];134135[_hud showAnimated:YES136whileExecutingBlock:^{137[self taskOfDeterminate];138} completionBlock:^{139NSLog(@"showHUDByDeterminate 执行完成");140}];141}142143- (void)showHUDByDeterminateHorizontalBar {144_hud =[[MBProgressHUD alloc] initWithView:self.view];145_hud.mode =MBProgressHUDModeDeterminateHorizontalBar;146[self.view addSubview:_hud];147148[_hud showAnimated:YES149whileExecutingBlock:^{150[self taskOfDeterminateHorizontalBar];151} completionBlock:^{152NSLog(@"showHUDByDeterminateHorizontalBar 执行完成");153}];154}155156- (void)showHUDByAnnularDeterminate {157_hud =[[MBProgressHUD alloc] initWithView:self.view];158_hud.mode =MBProgressHUDModeAnnularDeterminate;159[self.view addSubview:_hud];160161[_hud showAnimated:YES162whileExecutingBlock:^{163[self taskOfAnnularDeterminate];164} completionBlock:^{165NSLog(@"showHUDByAnnularDeterminate 执行完成");166}];167}168169- (void)showHUDByCustomView {170_hud =[[MBProgressHUD alloc] initWithView:self.view];171_hud.mode =MBProgressHUDModeCustomView;172_hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AlbumCellRedSelected"]];173[self.view addSubview:_hud];174175[_hud showAnimated:YES176whileExecutingBlock:^{177[self taskOfCustomView];178} completionBlock:^{179NSLog(@"showHUDByCustomView 执行完成");180}];181182}183184- (void)showHUDByText {185UIColor *color =[UIColor cyanColor];186187_hud =[[MBProgressHUD alloc] initWithView:self.view];188_hud.mode =MBProgressHUDModeText;189_hud.labelText =@"加载中...";190_hud.labelFont = [UIFont systemFontOfSize:17];191_hud.labelColor = color;//设置文本颜色;默认为白色192_hud.detailsLabelText =@"用户请稍候,耐心等待";193_hud.detailsLabelFont = [UIFont systemFontOfSize:14];194_hud.detailsLabelColor = color;//设置详细文本颜色;默认为白色195[self.view addSubview:_hud];196197[_hud showAnimated:YES198whileExecutingBlock:^{199[self taskOfText];200} completionBlock:^{201NSLog(@"showHUDByText 执行完成");202}];203}204205#pragmamark - MBProgressHUDDelegate206- (void)hudWasHidden:(MBProgressHUD *)hud {207NSLog(@"隐藏后做一些操作");208}209210#pragmamark - TableView211- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {212return@"展示模式列表";213}214215- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {216return1;217}218219- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {220return_arrMode.count;221}222223- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {224staticNSString *cellIdentifier =@"cellIdentifier";225UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];226if(!cell) {227cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];228}229230NSInteger row =indexPath.row;231cell.textLabel.text =_arrMode[row];232cell.textLabel.adjustsFontSizeToFitWidth =YES;233cell.detailTextLabel.text =_arrModeName[row];234cell.detailTextLabel.adjustsFontSizeToFitWidth =YES;235236returncell;237}238239- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {240switch(indexPath.row) {241case0:242[self showHUDByIndeterminate];243

break;244case1:245[self showHUDByDeterminate];246

break;247case2:248[self showHUDByDeterminateHorizontalBar];249

break;250case3:251[self showHUDByAnnularDeterminate];252

break;253case4:254[self showHUDByCustomView];255

break;256case5:257[self showHUDByText];258

break;259}260261NSLog(@"%ld", (long)indexPath.row);262}263264@end

AppDelegate.h

1#import<UIKit/UIKit.h>23@interfaceAppDelegate : UIResponder <UIapplicationDelegate>45@property (strong, nonatomic) UIWindow *window;6@property (strong, nonatomic) UINavigationController *navigationController;78@end

AppDelegate.m

1#import"AppDelegate.h"2#import"ViewController.h"34@interfaceAppDelegate ()56@end78@implementationAppDelegate91011- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {12_window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];13ViewController *viewController =[[ViewController alloc] init];14_navigationController =[[UINavigationController alloc] initWithRootViewController:viewController];15_window.rootViewController =_navigationController;16//[_window addSubview:_navigationController.view];//当_window.rootViewController关联时,这一句可有可无17[_window makeKeyAndVisible];18returnYES;19}2021- (void)applicationWillResignActive:(UIApplication *)application {22}2324- (void)applicationDidEnterBackground:(UIApplication *)application {25}2627- (void)applicationWillEnterForeground:(UIApplication *)application {28}2930- (void)applicationDidBecomeActive:(UIApplication *)application {31}3233- (void)applicationWillTerminate:(UIApplication *)application {34}3536@end

输出结果:

12015-07-1113:48:30.788MBProgressHUDDemo[7211:111189]022015-07-1113:48:36.090MBProgressHUDDemo[7211:111189] showHUDByIndeterminate 执行完成32015-07-1113:48:36.091MBProgressHUDDemo[7211:111189] 隐藏后做一些操作42015-07-1113:48:37.378MBProgressHUDDemo[7211:111189]152015-07-1113:48:43.208MBProgressHUDDemo[7211:111189] showHUDByDeterminate 执行完成62015-07-1113:48:44.435MBProgressHUDDemo[7211:111189]272015-07-1113:48:50.278MBProgressHUDDemo[7211:111189] showHUDByDeterminateHorizontalBar 执行完成82015-07-1113:48:51.692MBProgressHUDDemo[7211:111189]392015-07-1113:48:57.529MBProgressHUDDemo[7211:111189] showHUDByAnnularDeterminate 执行完成102015-07-1113:48:58.473MBProgressHUDDemo[7211:111189]4112015-07-1113:49:01.778MBProgressHUDDemo[7211:111189] showHUDByCustomView 执行完成122015-07-1113:49:02.790MBProgressHUDDemo[7211:111189]5132015-07-1113:49:06.096MBProgressHUDDemo[7211:111189] showHUDByText 执行完成

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航