效果一 只显示文字:
冯绍峰粉色粉色.gif
// 只显示文字- (void)TextButtonAction{self.HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:_HUD]; _HUD.labelText =@"加载中,请稍等..."; _HUD.mode = MBProgressHUDModeText; [_HUD showAnimated:YESwhileExecutingBlock:^{ sleep(2); } completionBlock:^{ [_HUD removeFromSuperview]; _HUD =nil; }];}
效果二 圆形进度条
冯绍峰粉色粉色.gif
// 圆形进度条- (void)CircularButtonAction{//圆形进度条self.HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:_HUD]; _HUD.mode = MBProgressHUDModeDeterminate; _HUD.delegate =self; _HUD.labelText =@"等待中"; [_HUD showWhileExecuting:@selector(ProgressBar) onTarget:selfwithObject:nilanimated:YES];}// 进度条 计算- (void)ProgressBar{// 进度指示器,从0.0到1.0,默认值为0.0floatprogress =0.0f;while(progress <1.0f) { progress +=0.01f; _HUD.progress = progress;// usleep函数能把进程挂起一段时间, 单位是微秒(千分之一毫秒)usleep(50000); }}
效果三 文字 加 菊花
冯绍峰粉色粉色.gif
// 通常情况 文字 加 菊花- (void)GeneralButtonAction{//初始化进度框,置于当前的View当中self.HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:_HUD];//如果设置此属性则当前的view置于后台_HUD.dimBackground =YES;//设置对话框文字_HUD.labelText =@"加载中";//细节文字_HUD.detailsLabelText =@"请耐心等待";//显示对话框[_HUD showAnimated:YESwhileExecutingBlock:^{//对话框显示时需要执行的操作sleep(3); }// 在HUD被隐藏后的回调completionBlock:^{//操作执行完后取消对话框[_HUD removeFromSuperview]; _HUD =nil; }];}
效果四 自定义动图
冯绍峰粉色粉色.gif
// 自定义 动图- (void)CustomButtonAction{//自定义viewself.HUD = [[MBProgressHUD alloc] initWithView:self.view];//取消背景框self.HUD.color = [UIColorwhiteColor]; [self.view addSubview:_HUD];UIImageView*images = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,200,300)];NSMutableArray*imageArray = [[NSMutableArrayalloc]init];for(inti =1; i <8; i++){NSString*imgName = [NSStringstringWithFormat:@"%d.tiff",i]; [imageArray addObject:[UIImageimageNamed:imgName]]; } images.animationDuration =0.7; images.animationImages = imageArray;// 开始播放[images startAnimating];//自定义_HUD.mode = MBProgressHUDModeCustomView; _HUD.delegate =self; _HUD.customView = images; [_HUD show:YES];//延迟[_HUD hide:YESafterDelay:2]; }
网友评论