MBProgressHUD的基本使用

 

MBProgressHUD的基本使用

分类: IOS

和gitHub上的Demo其实差不多,就是小整理了下,当备忘,想做复杂的效果可以参考MBProgressHUD在gitHub上的DEMO,写得也很清楚明了。

先下载MBProgressHUD.h和.m文件,拖入工程。地址:MBProgressHUD

以下是代码:(先在.h文件里定义 MBProgressHUD *HUD;)

  1. //方式1.直接在View上show  
  2. HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];  
  3. HUD.delegate = self;  
  4.   
  5. //常用的设置  
  6. //小矩形的背景色  
  7. HUD.color = [UIColor clearColor];//这儿表示无背景  
  8. //显示的文字  
  9. HUD.labelText = @"Test";  
  10. //细节文字  
  11. HUD.detailsLabelText = @"Test detail";  
  12. //是否有庶罩  
  13. HUD.dimBackground = YES;  
  14. [HUD hide:YES afterDelay:2];  
  15.   
  16. //只显示文字  
  17. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];  
  18. hud.mode = MBProgressHUDModeText;  
  19. hud.labelText = @"Some message...";  
  20. hud.margin = 10.f;  
  21. hud.yOffset = 150.f;  
  22. hud.removeFromSuperViewOnHide = YES;  
  23. [hud hide:YES afterDelay:3];  
  24.   
  25. //方式2.initWithView  
  26. //use block  
  27. HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  28. [self.view addSubview:HUD];  
  29. HUD.labelText = @"Test";  
  30. [HUD showAnimated:YES whileExecutingBlock:^{  
  31.     NSLog(@"%@",@"do somethings....");  
  32.     [self doTask];  
  33. } completionBlock:^{  
  34.     [HUD removeFromSuperview];  
  35.     [HUD release];          
  36. }];  
  37.   
  38. //圆形进度条  
  39. HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  40. [self.view addSubview:HUD];  
  41. HUD.mode = MBProgressHUDModeAnnularDeterminate;  
  42. HUD.delegate = self;  
  43. HUD.labelText = @"Loading";  
  44. [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];  
  45.   
  46. //自定义view  
  47. HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  48. HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];  
  49. // Set custom view mode  
  50. HUD.mode = MBProgressHUDModeCustomView;  
  51. HUD.delegate = self;  
  52. HUD.labelText = @"Completed";  
  53. [HUD show:YES];  
  54. [HUD hide:YES afterDelay:3];  

代理方法:

  1. #pragma mark -  
  2. #pragma mark HUD的代理方法,关闭HUD时执行  
  3. -(void)hudWasHidden:(MBProgressHUD *)hud  
  4. {  
  5.     [hud removeFromSuperview];  
  6.     [hud release];  
  7.     hud = nil;  
  8. }  

二个task

  1.   -(void) doTask{  
  2.     //你要进行的一些逻辑操作  
  3.     sleep(2);  
  4.   }  
  5.   
  6.   -(void) myProgressTask{  
  7.        float progress = 0.0f;  
  8. while (progress < 1.0f) {  
  9.     progress += 0.01f;  
  10.     HUD.progress = progress;  
  11.     usleep(50000);  
  12. }  
  13.   
  14.   }  
原文地址:https://www.cnblogs.com/iOS-mt/p/4172704.html