MBProgressHUD 使用简介

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

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

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

  1 //方式1.直接在View上show  
  2 
  3 HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];  
  4 
  5 
  6 HUD.delegate = self;  
  7 
  8 //常用的设置  
  9 
 10 //小矩形的背景色  
 11 
 12 HUD.color = [UIColor clearColor];//这儿表示无背景  
 13 
 14 //显示的文字  
 15 
 16 HUD.labelText = @"Test";  
 17 
 18 //细节文字  
 19 
 20 HUD.detailsLabelText = @"Test detail";  
 21 
 22 //是否有庶罩  
 23 
 24 HUD.dimBackground = YES;  
 25 
 26 [HUD hide:YES afterDelay:2];  
 27 
 28 //只显示文字  
 29 
 30 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];  
 31 
 32 hud.mode = MBProgressHUDModeText;  
 33 
 34 hud.labelText = @"Some message...";  
 35 
 36 hud.margin = 10.f;  
 37 
 38 hud.yOffset = 150.f;  
 39 
 40 hud.removeFromSuperViewOnHide = YES;  
 41 
 42 [hud hide:YES afterDelay:3];  
 43 


44 //方式2.initWithView 45 46 //use block 47 48 HUD = [[MBProgressHUD alloc] initWithView:self.view]; 49 50 [self.view addSubview:HUD]; 51 52 HUD.labelText = @"Test"; 53 54 [HUD showAnimated:YES whileExecutingBlock:^{ 55 56 NSLog(@"%@",@"do somethings...."); 57 58 [self doTask]; 59 60 } completionBlock:^{ 61 62 [HUD removeFromSuperview]; 63 64 [HUD release]; 65 66 }]; 67 68 //圆形进度条 69 70 HUD = [[MBProgressHUD alloc] initWithView:self.view]; 71 72 [self.view addSubview:HUD]; 73 74 HUD.mode = MBProgressHUDModeAnnularDeterminate; 75 76 HUD.delegate = self; 77 78 HUD.labelText = @"Loading"; 79 80 [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES]; 81 82 //自定义view 83 84 HUD = [[MBProgressHUD alloc] initWithView:self.view]; 85 86 HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease]; 87 88 // Set custom view mode 89 90 HUD.mode = MBProgressHUDModeCustomView; 91 92 HUD.delegate = self; 93 94 HUD.labelText = @"Completed"; 95 96 [HUD show:YES]; 97 98 [HUD hide:YES afterDelay:3]; 99 100 //代理方法: 101 102 #pragma mark - 103 104 #pragma mark HUD的代理方法,关闭HUD时执行 105 106 -(void)hudWasHidden:(MBProgressHUD *)hud 107 108 { 109 110 [hud removeFromSuperview]; 111 112 [hud release]; 113 114 hud = nil; 115 116 } 117 118 二个task 119 120 121 122 123 124 125 -(void) doTask{ 126 127 //你要进行的一些逻辑操作 128 129 sleep(2); 130 131 } 132 133 -(void) myProgressTask{ 134 135 float progress = 0.0f; 136 137 while (progress < 1.0f) { 138 139 progress += 0.01f; 140 141 HUD.progress = progress; 142 143 usleep(50000); 144 145 } 146 147 }

用起来还是很easy的。

原文地址:https://www.cnblogs.com/song-kl/p/4576995.html