1,将MBProgressHUD.h 和MBProgressHUB.m 两个文件添加到项目当中:
2,开始相关编码:
// // ViewController.m // MBProgressHUDDemo // // Created by 陈海涛 on 13-7-29. // Copyright (c) 2013年陈海涛. All rights reserved. // #import "ViewController.h" #import "MBProgressHUD.h" #import "ASIHTTPRequest.h" @interface ViewController () //HUD(Head-Up Display, 意思是抬头显示的意思) @property (nonatomic,strong) MBProgressHUD *hud; @end @implementation ViewController - (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)showTextAction:(id)sender { //初始化进度框,设置于当前的view中 self.hud = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:self.hud]; //设置背景为暗淡得 self.hud.dimBackground = YES; //设置对话框文字 self.hud.labelText = @"加载中。。。。"; self.hud.mode = MBProgressHUDModeIndeterminate; [self.hudshow:YES]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString: @"https://www.ifeng.com"]]; [NSURLConnectionsendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSLog(@"%@,%@",response,data); dispatch_async(dispatch_get_main_queue(), ^{ [self.hudremoveFromSuperview]; self.hud = nil; }); }]; }); } - (IBAction)showProgressAction1:(id)sender { self.hud = [[MBProgressHUDalloc] initWithView:self.view]; [self.view addSubview:self.hud]; //设置hud的出现的动画效果 self.hud.animationType = MBProgressHUDAnimationZoom; self.hud.removeFromSuperViewOnHide = YES; self.hud.mode =MBProgressHUDModeIndeterminate; self.hud.labelText = @"开始。。。"; [self.hudshow:YES]; ASIHTTPRequest *request = [[ASIHTTPRequestalloc] initWithURL:[NSURLURLWithString:@"https://github.com/matej/MBProgressHUD/zipball/master"]]; __blockASIHTTPRequest *request2 = request; [request setHeadersReceivedBlock:^(NSDictionary *responseHeaders) { self.hud.mode = MBProgressHUDModeDeterminate; self.hud.labelText = @"下载。。。"; [request2 setDownloadProgressDelegate:self.hud]; }]; [request setCompletionBlock:^{ NSLog(@"success"); self.hud.mode = MBProgressHUDModeCustomView; self.hud.labelText = @"成功。。。"; self.hud.customView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"Checkmark.png"]]; [self.hudhide:YES afterDelay:1]; [request2.responseDatawriteToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"123.zip"] atomically:YES]; }]; [request setFailedBlock:^{ NSLog(@"fail"); self.hud.mode = MBProgressHUDModeCustomView; self.hud.labelText = @"失败。。。"; self.hud.customView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"fail.jpeg"]]; [self.hudhide:YES afterDelay:1]; }]; [request startAsynchronous]; } - (IBAction)showProgressAction2:(id)sender { self.hud = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:self.hud]; self.hud.labelText = @"正在下载,请稍等"; self.hud.color = [UIColor grayColor]; self.hud.mode = MBProgressHUDModeAnnularDeterminate; [self.hud show:YES]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"https://github.com/matej/MBProgressHUD/zipball/master"]]; request.downloadProgressDelegate = self.hud; [request startAsynchronous]; } @end
[/size]