iOS 进度条的实现
jopen
12年前
在iOS的开发当中,经常会遇到读取系统资源等类似的情况,如果网络比较卡的话,用户很可能以为这个app已经挂掉了,用户体验很差,老外还是很好的,提供开源的source,跟大家一块学习下。
iOS的进度条可以分为几类,有普通的,就像一个圈圈在那转,有在圈圈下加文字的,有直接是纯文字的,等等。。
在自己的项目中需要加入以下2个文件:MBProgressHUD.h和MBProgressHUD.m;接下来我们只需要在我们的.m文件中引用progress的头文件即可。
对于普通的进度条,代码如下:
- (IBAction)showSimple:(id)sender { // The hud will dispable all input on the view (use the higest view possible in the view hierarchy) HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD]; // Regiser for HUD callbacks so we can remove it from the window at the right time HUD.delegate = self; // Show the HUD while the provided method executes in a new thread [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES]; }选择器函数myTask中可以做我们想要做的事情:
- (void)myTask { // Do something usefull in here instead of sleeping ... sleep(3); }
效果如图:
带有文字的进度条:
- (IBAction)showWithLabel:(id)sender { HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD]; HUD.delegate = self; HUD.labelText = @"Loading"; [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES]; }效果图如下:
纯文字的代码:
- (IBAction)showTextOnly:(id)sender { MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; // Configure for text only and offset down hud.mode = MBProgressHUDModeText; hud.labelText = @"Some message..."; hud.margin = 10.f; hud.yOffset = 150.f; hud.removeFromSuperViewOnHide = YES; [hud hide:YES afterDelay:3]; }
效果图如下:
参考地址:https://github.com/jdg/MBProgressHUD
来自:http://blog.csdn.net/weasleyqi/article/details/8072897