MBProgressHUD的基本使用

jopen 9年前發布 | 1K 次閱讀 Objective-C IOS

和gitHub上的Demo其實差不多,就是小整理了下,當備忘,想做復雜的效果可以參考MBProgressHUD在gitHub上的DEMO,寫得也很清楚明了。

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

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

    //方式1.直接在View上show
HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
HUD.delegate = self;

//常用的設置  
//小矩形的背景色  
HUD.color = [UIColor clearColor];//這兒表示無背景  
//顯示的文字  
HUD.labelText = @"Test";  
//細節文字  
HUD.detailsLabelText = @"Test detail";  
//是否有庶罩  
HUD.dimBackground = YES;  
[HUD hide:YES afterDelay:2];  

//只顯示文字  
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];  
hud.mode = MBProgressHUDModeText;  
hud.labelText = @"Some message...";  
hud.margin = 10.f;  
hud.yOffset = 150.f;  
hud.removeFromSuperViewOnHide = YES;  
[hud hide:YES afterDelay:3];  

//方式2.initWithView  
//use block  
HUD = [[MBProgressHUD alloc] initWithView:self.view];  
[self.view addSubview:HUD];  
HUD.labelText = @"Test";  
[HUD showAnimated:YES whileExecutingBlock:^{  
    NSLog(@"%@",@"do somethings....");  
    [self doTask];  
} completionBlock:^{  
    [HUD removeFromSuperview];  
    [HUD release];          
}];  

//圓形進度條  
HUD = [[MBProgressHUD alloc] initWithView:self.view];  
[self.view addSubview:HUD];  
HUD.mode = MBProgressHUDModeAnnularDeterminate;  
HUD.delegate = self;  
HUD.labelText = @"Loading";  
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];  

//自定義view  
HUD = [[MBProgressHUD alloc] initWithView:self.view];  
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];  
// Set custom view mode  
HUD.mode = MBProgressHUDModeCustomView;  
HUD.delegate = self;  
HUD.labelText = @"Completed";  
[HUD show:YES];  
[HUD hide:YES afterDelay:3];  </pre> 


代理方法:

    #pragma mark -

#pragma mark HUD的代理方法,關閉HUD時執行  
-(void)hudWasHidden:(MBProgressHUD *)hud  
{  
    [hud removeFromSuperview];  
    [hud release];  
    hud = nil;  
}  </pre> 


二個task

 -(void) doTask{
//你要進行的一些邏輯操作
sleep(2);
}

-(void) myProgressTask{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
usleep(50000);
}

} </pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!