iOS 第三方类库之MBProgressHUD

github链接地址

MBProgressHUD是一个开源的第三方类库实现了很多种样式的提示框,类似Activity indicator,使用上简单、方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到

      MBProgressHUD is an iOS drop-in class that displays a translucent HUD with an indicator and/or labels while work is being done in a background thread. The HUD is meant as a replacement for the undocumented, private UIKit UIProgressHUD with some additional features.

 

Usage

 

The main guideline you need to follow when dealing with MBProgressHUD while running long-running tasks is keeping the main thread work-free, so the UI can be updated promptly. The recommended way of using MBProgressHUD is therefore to set it up on the main thread and then spinning the task, that you want to perform, off onto a new thread.

程序在执行一个长时间任务的时候(例如网络请求),可以使用该类库在主窗口中添加一个提示框显示进度或者相关的提示信息。

 

  1. [MBProgressHUD showHUDAddedTo:self.view animated:YES];  
  2. dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{  
  3.     // Do something...  
  4.     dispatch_async(dispatch_get_main_queue(), ^{  
  5.         [MBProgressHUD hideHUDForView:self.view animated:YES];  
  6.     });  
  7. });  

 

在这个类库中,提供了丰富的设置方法,可以构建出令人满意的提示框,在github下载这个项目就包括了一个demo,里面涵盖了所以基本的设置方法。

下面简单的记录一下。

//新建一个HUD添加到视图中

MBProgressHUD *HUD;

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];

// Regiser for HUD callbacks so we can remove it from the window at the right time设置代理
HUD.delegate = self;

// Show the HUD while the provided method executes in a new thread 显示HUD的同时执行其他的操作
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];

//设置相关的文本提示信息

HUD.labelText = @"Loading";

HUD.detailsLabelText = @"updating data";

//设置HUD的颜色  Set the hud to display with a color

HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];

// Set determinate mode
HUD.mode = MBProgressHUDModeDeterminate;

//HUD的mode包括以下几种:

typedef enum {
/** Progress is shown using an UIActivityIndicatorView. This is the default. */  (默认是UIActivityIndicatorView样式
MBProgressHUDModeIndeterminate,
     
/**Progress is shown using a round, pie-chart like, progress view. */  (类似饼状图)
MBProgressHUDModeDeterminate,
/**Progress is shown using a ring-shaped progress view. */  (类似圆环)
MBProgressHUDModeAnnularDeterminate,
/**Shows a custom view */  (自定义)
MBProgressHUDModeCustomView,
/** Shows only labels */  (纯文本)
MBProgressHUDModeText
} MBProgressHUDMode;

当mode选择custom时,需要设置custom view

HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] ;

使用Blocks

[hud showAnimated:YES whileExecutingBlock:^{
[self myTask]; //hud显示过程中执行的其他操作
} completionBlock:^{
[hud removeFromSuperview];  
}];

// 设置HUD显示所在的视图背景暗淡

HUD.dimBackground = YES;

//设置HUD的进度

HUD.progress

//其他

- (void)show:(BOOL)animated;

- (void)hide:(BOOL)animated;

- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;

原文地址:https://www.cnblogs.com/yulang314/p/3551312.html