iOS 进度条的实现

在iOS的开发当中,经常会遇到读取系统资源等类似的情况,如果网络比较卡的话,用户很可能以为这个app已经挂掉了,用户体验很差,老外还是很好的,提供开源的source,跟大家一块学习下。

iOS的进度条可以分为几类,有普通的,就像一个圈圈在那转,有在圈圈下加文字的,有直接是纯文字的,等等。。

在自己的项目中需要加入以下2个文件:MBProgressHUD.h和MBProgressHUD.m;接下来我们只需要在我们的.m文件中引用progress的头文件即可。

对于普通的进度条,代码如下:

 

  1. - (IBAction)showSimple:(id)sender {  
  2.     // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)  
  3.     HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];  
  4.     [self.navigationController.view addSubview:HUD];  
  5.       
  6.     // Regiser for HUD callbacks so we can remove it from the window at the right time  
  7.     HUD.delegate = self;  
  8.       
  9.     // Show the HUD while the provided method executes in a new thread  
  10.     [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];  
  11. }  

选择器函数myTask中可以做我们想要做的事情:

  1. - (void)myTask {  
  2.     // Do something usefull in here instead of sleeping ...  
  3.     sleep(3);  
  4. }  

 

带有文字的进度条:

  1. - (IBAction)showWithLabel:(id)sender {  
  2.       
  3.     HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];  
  4.     [self.navigationController.view addSubview:HUD];  
  5.       
  6.     HUD.delegate = self;  
  7.     HUD.labelText = @"Loading";  
  8.       
  9.     [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];  
  10. }  

纯文字的代码:

    1. - (IBAction)showTextOnly:(id)sender {  
    2.       
    3.     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];  
    4.       
    5.     // Configure for text only and offset down  
    6.     hud.mode = MBProgressHUDModeText;  
    7.     hud.labelText = @"Some message...";  
    8.     hud.margin = 10.f;  
    9.     hud.yOffset = 150.f;  
    10.     hud.removeFromSuperViewOnHide = YES;  
    11.       
    12.     [hud hide:YES afterDelay:3];  

posted on 2012-11-06 16:14  wencansz  阅读(532)  评论(0编辑  收藏  举报