iOS——MBProgressHUD的使用方法

    xiaoxiao2021-12-15  41

    MBProgressHUD就是一个非常好用的第三方库,可以快速接入加载的界面,下面贴代码,下面的代码包含了各种常用类型的加载:

    #import "ViewController.h" #import "MBProgressHUD.h" @interface ViewController () @property (atomic, assign) BOOL canceled; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } //仅显示加载的图标 - (IBAction)indeterminateExample:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWork]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); }); } - (IBAction)With_details_label:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); hud.detailsLabel.text = NSLocalizedString(@"Parsing data\n(1/1)", @"HUD title"); dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWork]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); }); } - (IBAction)With_label:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.label.text = @"正在加载"; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWork]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); }); } - (IBAction)Circle_style:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeDeterminate; hud.label.text = @"正在加载"; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); }); } - (IBAction)Progress_style:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeDeterminateHorizontalBar; hud.label.text = @"正在加载"; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); }); } - (IBAction)Text_only:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeText; hud.label.text = @"正在加载"; hud.offset = CGPointMake(0.f, MBProgressMaxOffset); [hud hideAnimated:YES afterDelay:3.f]; } - (IBAction)With_action_button:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeDeterminate; hud.label.text = @"正在加载"; [hud.button setTitle:@"取消" forState:UIControlStateNormal]; [hud.button addTarget:self action:@selector(cancelWork:) forControlEvents:UIControlEventTouchUpInside]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); }); } - (IBAction)Custom_view:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeCustomView; UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; hud.customView = [[UIImageView alloc] initWithImage:image]; hud.square = YES; hud.label.text = @"已完成"; [hud hideAnimated:YES afterDelay:1.5f]; } - (void)doSomeWorkWithProgress { self.canceled = NO; float progress = 0.0f; while (progress < 1.0f) { if (self.canceled) break; progress += 0.01f; dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD HUDForView:self.view].progress = progress; }); usleep(50000); } } -(void)doSomeWork { //加载时候处理的事件和方法 NSLog(@"正在加载"); //显示加载的时间 sleep(1.5); } - (void)cancelWork:(id)sender { self.canceled = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 以上是全部代码,效果图如下:

    上面的源码博主有上传到github上,若感兴趣可以点击前往下载。

    转载请注明原文地址: https://ju.6miu.com/read-1000182.html

    最新回复(0)