iOS 进度框(一) MBProgressHUD
来源:互联网 发布:php exec w3c 编辑:程序博客网 时间:2024/06/05 18:52
MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单、方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到。
GitHub项目源码 https://github.com/jdg/MBProgressHUD
下载下来后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,别忘了选择拷贝到工程。
源码讲解:
1、当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象时执行awakeFromNib。
该方法定义了一个二维数组,每一个元素都是一种 “提示框” 的类。
- (void)awakeFromNib { [super awakeFromNib]; self.examples = @[@[[MBExample exampleWithTitle:@"Indeterminate mode" selector:@selector(indeterminateExample)], [MBExample exampleWithTitle:@"With label" selector:@selector(labelExample)], [MBExample exampleWithTitle:@"With details label" selector:@selector(detailsLabelExample)]], @[[MBExample exampleWithTitle:@"Determinate mode" selector:@selector(determinateExample)], [MBExample exampleWithTitle:@"Annular determinate mode" selector:@selector(annularDeterminateExample)], [MBExample exampleWithTitle:@"Bar determinate mode" selector:@selector(barDeterminateExample)]], @[[MBExample exampleWithTitle:@"Text only" selector:@selector(textExample)], [MBExample exampleWithTitle:@"Custom view" selector:@selector(customViewExample)], [MBExample exampleWithTitle:@"With action button" selector:@selector(cancelationExample)], [MBExample exampleWithTitle:@"Mode switching" selector:@selector(modeSwitchingExample)]], @[[MBExample exampleWithTitle:@"On window" selector:@selector(indeterminateExample)], [MBExample exampleWithTitle:@"NSURLSession" selector:@selector(networkingExample)], [MBExample exampleWithTitle:@"Dim background" selector:@selector(dimBackgroundExample)], [MBExample exampleWithTitle:@"Colored" selector:@selector(colorExample)]] ];}2、模拟耗时操作
- (void)doSomeWork { // Simulate by just waiting. sleep(3.);}- (void)doSomeWorkWithProgress { self.canceled = NO; // This just increases the progress indicator in a loop. float progress = 0.0f; while (progress < 1.0f) { if (self.canceled) break; progress += 0.01f; dispatch_async(dispatch_get_main_queue(), ^{ // Instead we could have also passed a reference to the HUD // to the HUD to myProgressTask as a method parameter. [MBProgressHUD HUDForView:self.navigationController.view].progress = progress; }); usleep(50000); }}- (void)doSomeWorkWithMixedProgress { MBProgressHUD *hud = [MBProgressHUD HUDForView:self.navigationController.view]; // Indeterminate mode sleep(2); // Switch to determinate mode dispatch_async(dispatch_get_main_queue(), ^{ hud.mode = MBProgressHUDModeDeterminate; hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); }); float progress = 0.0f; while (progress < 1.0f) { progress += 0.01f; dispatch_async(dispatch_get_main_queue(), ^{ hud.progress = progress; }); usleep(50000); } // Back to indeterminate mode dispatch_async(dispatch_get_main_queue(), ^{ hud.mode = MBProgressHUDModeIndeterminate; hud.label.text = NSLocalizedString(@"Cleaning up...", @"HUD cleanining up title"); }); sleep(2); dispatch_sync(dispatch_get_main_queue(), ^{ UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; hud.customView = imageView; hud.mode = MBProgressHUDModeCustomView; hud.label.text = NSLocalizedString(@"Completed", @"HUD completed title"); }); sleep(2);}- (void)doSomeNetworkWorkWithProgress { NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; NSURL *URL = [NSURL URLWithString:@"https://support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1425/sample_iPod.m4v.zip"]; NSURLSessionDownloadTask *task = [session downloadTaskWithURL:URL]; [task resume];}3、项目Demo运行截图
4、功能函数
(1.1)Indeterminate mode(和 "On window" 代码一样)
- (void)indeterminateExample { // Show the HUD on the root view (self.view is a scrollable table view and thus not suitable, // as the HUD would move with the content as we scroll). MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Fire off an asynchronous task, giving UIKit the opportunity to redraw wit the HUD added to the // view hierarchy. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background [self doSomeWork]; // IMPORTANT - Dispatch back to the main thread. Always access UI // classes (including MBProgressHUD) on the main thread. dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(1.2)With label
- (void)labelExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the label text. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); // You can also adjust other label properties if needed. // hud.label.font = [UIFont italicSystemFontOfSize:16.f]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ [self doSomeWork]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(1.3)With details label
- (void)detailsLabelExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the label text. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); // Set the details label text. Let's make it multiline this time. 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]; }); });}
(2.1)Determinate mode
- (void)determinateExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the determinate mode to show task progress. hud.mode = MBProgressHUDModeDeterminate; hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background and update the HUD periodically. [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(2.2)Annular determinate mode
- (void)annularDeterminateExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the annular determinate mode to show task progress. hud.mode = MBProgressHUDModeAnnularDeterminate; hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background and update the HUD periodically. [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(2.3)Bar determinate mode
- (void)barDeterminateExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the bar determinate mode to show task progress. hud.mode = MBProgressHUDModeDeterminateHorizontalBar; hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background and update the HUD periodically. [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(3.1)Text only
- (void)textExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the annular determinate mode to show task progress. hud.mode = MBProgressHUDModeText; hud.label.text = NSLocalizedString(@"Message here!", @"HUD message title"); // Move to bottm center. hud.offset = CGPointMake(0.f, MBProgressMaxOffset); [hud hideAnimated:YES afterDelay:3.f];}
(3.2)Custom view
- (void)customViewExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the custom view mode to show any view. hud.mode = MBProgressHUDModeCustomView; // Set an image view with a checkmark. UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; hud.customView = [[UIImageView alloc] initWithImage:image]; // Looks a bit nicer if we make it square. hud.square = YES; // Optional label text. hud.label.text = NSLocalizedString(@"Done", @"HUD done title"); [hud hideAnimated:YES afterDelay:3.f];}
(3.3)With action button
- (void)cancelationExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set the determinate mode to show task progress. hud.mode = MBProgressHUDModeDeterminate; hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); // Configure the button. [hud.button setTitle:NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal]; [hud.button addTarget:self action:@selector(cancelWork:) forControlEvents:UIControlEventTouchUpInside]; dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background and update the HUD periodically. [self doSomeWorkWithProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(3.4)Mode switching
- (void)modeSwitchingExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set some text to show the initial status. hud.label.text = NSLocalizedString(@"Preparing...", @"HUD preparing title"); // Will look best, if we set a minimum size. hud.minSize = CGSizeMake(150.f, 100.f); dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background and update the HUD periodically. [self doSomeWorkWithMixedProgress]; dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(4.1)On window(和 "Indeterminate mode" 代码一样)
- (void)indeterminateExample { // Show the HUD on the root view (self.view is a scrollable table view and thus not suitable, // as the HUD would move with the content as we scroll). MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Fire off an asynchronous task, giving UIKit the opportunity to redraw wit the HUD added to the // view hierarchy. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ // Do something useful in the background [self doSomeWork]; // IMPORTANT - Dispatch back to the main thread. Always access UI // classes (including MBProgressHUD) on the main thread. dispatch_async(dispatch_get_main_queue(), ^{ [hud hideAnimated:YES]; }); });}
(4.2)NSURL Session
- (void)networkingExample { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Set some text to show the initial status. hud.label.text = NSLocalizedString(@"Preparing...", @"HUD preparing title"); // Will look best, if we set a minimum size. hud.minSize = CGSizeMake(150.f, 100.f); [self doSomeNetworkWorkWithProgress];}
(4.3)Dim background
- (void)dimBackgroundExample {MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];// Change the background view style and color.hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.1f];dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{[self doSomeWork];dispatch_async(dispatch_get_main_queue(), ^{[hud hideAnimated:YES];});});}
(4.4)Colored
- (void)colorExample {MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];hud.contentColor = [UIColor colorWithRed:0.f green:0.6f blue:0.7f alpha:1.f];// Set the label text.hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{[self doSomeWork];dispatch_async(dispatch_get_main_queue(), ^{[hud hideAnimated:YES];});});}
0 0
- iOS 进度框(一) MBProgressHUD
- iOS进度提示组件 MBProgressHUD、SVProgressHUD
- ios MBProgressHUD
- ios MBProgressHUD
- SVProgressHUD–比MBProgressHUD更好用的 iOS进度提示组件
- IOS-- toast提示框(MBProgressHUD)
- iOS提示框之 MBProgressHUD 与 SVProgressHUD
- MBProgressHUD(进度指示符库)
- 第三方进度显示器MBProgressHUD的使用
- iOS自定义对话框-MBProgressHUD
- iOS MBProgressHUD的使用
- MBProgressHUD的使用-ios
- iOS:开源类MBProgressHUD
- 【iOS开发】---- 开源库MBProgressHUD
- iOS MBProgressHUD 的使用方法
- iOS开发之MBProgressHUD
- iOS 源代码分析 --- MBProgressHUD
- iOS MBProgressHUD使用
- 运动目标跟踪(二)--搜索算法预测模型之粒子滤波
- springboot-aop访问拦截
- 类——类的复用(组合、继承、代理)
- Android 观察者与被观察者
- 错误处理页例子
- iOS 进度框(一) MBProgressHUD
- 运动目标跟踪(三)--搜索算法优化搜索方向之Meanshift
- 详细探究Spark的shuffle实现和hadoop mapreduce shuffle原理
- html的meta总结
- Java NIO Fundamentals
- 【java多线程系列】java内存模型与指令重排序
- 进度条代码的编写
- Android 系统稳定性 - ANR(一)
- 【bzoj1492】【NOI2007】【货币兑换】【斜率优化+cdq分治】