iOS開發之MBProgressHUD的使用

MBProgressHUD是iOS中的一個第三方庫,主要是在界面上顯示一個加載的進度框或者提示框,如下圖所示:

              

下面就記錄一下使用MBProgressHUD的方法:

1、導入MBProgressHUD到項目中

這裏使用cocoapods導入,Podfile文件的內容如下:


如果不清楚MBProgressHUD的版本是多少,可以在終端下執行pod search MBProgressHUD命令,即可顯示出當前的MBProgressHUD的最新版本,如下圖所示:


2、在代碼中使用MBProgressHUD

首先在頭文件中聲明一個MBProgressHUD變量,需要引入相應的頭文件,ViewController.h文件的代碼如下:

[objc] view plain copy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.     //初始化MBProgressHUD  
  12.     self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];  
  13. //    self.progressHUD.mode = MBProgressHUDModeIndeterminate;  
  14.     self.progressHUD.progress = 0.4;  
  15.     //添加ProgressHUD到界面中  
  16.     [self.view addSubview:self.progressHUD];  
  17. }  
  18.   
  19. #pragma mark - 顯示進度框  
  20. -(void)showProgress:(id)sender {  
  21.     self.progressHUD.dimBackground = NO//設置有遮罩  
  22.     self.progressHUD.labelText = @"加載中..."//設置進度框中的提示文字  
  23.     [self.progressHUD show:YES]; //顯示進度框  
  24. }  
  25.   
  26. - (void)didReceiveMemoryWarning {  
  27.     [super didReceiveMemoryWarning];  
  28.     // Dispose of any resources that can be recreated.  
  29. }  
  30.   
  31. @end  

其中,MBProgressHUD有一些配置項,下面分別說明:

(1)self.progressHUD.dimBackground配置項。該項主要配置對話框是否有遮罩,取值爲YES  /   NO,下面兩張圖是該配置項的區別:

        

(2)self.progressHUD.mode配置項。該配置項有6種不同的取值,分別對應6中不同形狀的進度框,取值有下面6種:

[objc] view plain copy
  1. typedef NS_ENUM(NSInteger, MBProgressHUDMode) {  
  2.     /** Progress is shown using an UIActivityIndicatorView. This is the default. */  
  3.     MBProgressHUDModeIndeterminate,  
  4.     /** Progress is shown using a round, pie-chart like, progress view. */  
  5.     MBProgressHUDModeDeterminate,  
  6.     /** Progress is shown using a horizontal progress bar */  
  7.     MBProgressHUDModeDeterminateHorizontalBar,  
  8.     /** Progress is shown using a ring-shaped progress view. */  
  9.     MBProgressHUDModeAnnularDeterminate,  
  10.     /** Shows a custom view */  
  11.     MBProgressHUDModeCustomView,  
  12.     /** Shows only labels */  
  13.     MBProgressHUDModeText  
  14. };  
對應的進度框如下圖所示:

      

       

還有一種mode爲MBProgressHUDModeCustomView,即自定義的View。

隱藏進度框需要調用下面的方法:

[objc] view plain copy
  1. [self.progressHUD hide:YES];  
  2. [self.progressHUD hide:YES afterDelay:5];  
其中第一個方法是立即隱藏進度框,第二個方法是延遲5秒再隱藏進度框。

3、self.progressHUD.progress屬性。該屬性配置的是進度框中顯示的進度,取值爲0-1




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章