HUD刷新

MBProgressHUD是一個開源項目,實現了很多種樣式的提示框,使用上簡單、方便,並且可以對顯示的內容進行自定義,功能很強大,很多項目中都有使用到。到GitHub上可以下載到項目源碼https://github.com/jdg/MBProgressHUD,下載下來後直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,別忘了選擇拷貝到工程。完了在需要使用的地方導入頭文件就可以開始使用了。首先看下工程截圖:

                                                                

接下來是整個Demo的完整界面,這裏我只選擇出了幾個常用的對話框,其他樣式的在源碼提供的Demo裏可以找到,要用的話直接參考就可以。

                                                                        

接下來直接上代碼了,頭文件部分:

  1. #import <UIKit/UIKit.h>  
  2. #import "MBProgressHUD.h"  
  3.   
  4. @interface ViewController : UIViewController  
  5. {  
  6.     //HUD(Head-Up Display,意思是擡頭顯示的意思)  
  7.     MBProgressHUD *HUD;  
  8. }  
  9.   
  10. - (IBAction)showTextDialog:(id)sender;  
  11. - (IBAction)showProgressDialog:(id)sender;  
  12. - (IBAction)showProgressDialog2:(id)sender;  
  13. - (IBAction)showCustomDialog:(id)sender;  
  14. - (IBAction)showAllTextDialog:(id)sender;  
  15.   
  16. @end  

實現文件(按鈕實現部分):

  1. - (IBAction)showTextDialog:(id)sender {  
  2.     //初始化進度框,置於當前的View當中  
  3.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  4.     [self.view addSubview:HUD];  
  5.       
  6.     //如果設置此屬性則當前的view置於後臺  
  7.     HUD.dimBackground = YES;  
  8.       
  9.     //設置對話框文字  
  10.     HUD.labelText = @"請稍等";  
  11.       
  12.     //顯示對話框  
  13.     [HUD showAnimated:YES whileExecutingBlock:^{  
  14.         //對話框顯示時需要執行的操作  
  15.         sleep(3);  
  16.     } completionBlock:^{  
  17.         //操作執行完後取消對話框  
  18.         [HUD removeFromSuperview];  
  19.         [HUD release];  
  20.         HUD = nil;  
  21.     }];  
  22. }  
  23.   
  24. - (IBAction)showProgressDialog:(id)sender {  
  25.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  26.     [self.view addSubview:HUD];  
  27.     HUD.labelText = @"正在加載";  
  28.       
  29.     //設置模式爲進度框形的  
  30.     HUD.mode = MBProgressHUDModeDeterminate;  
  31.     [HUD showAnimated:YES whileExecutingBlock:^{  
  32.         float progress = 0.0f;  
  33.         while (progress < 1.0f) {  
  34.             progress += 0.01f;  
  35.             HUD.progress = progress;  
  36.             usleep(50000);  
  37.         }  
  38.     } completionBlock:^{  
  39.         [HUD removeFromSuperview];  
  40.         [HUD release];  
  41.         HUD = nil;  
  42.     }];  
  43. }  
  44.   
  45. - (IBAction)showProgressDialog2:(id)sender {  
  46.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  47.     [self.view addSubview:HUD];  
  48.     HUD.labelText = @"正在加載";  
  49.     HUD.mode = MBProgressHUDModeAnnularDeterminate;  
  50.       
  51.     [HUD showAnimated:YES whileExecutingBlock:^{  
  52.         float progress = 0.0f;  
  53.         while (progress < 1.0f) {  
  54.             progress += 0.01f;  
  55.             HUD.progress = progress;  
  56.             usleep(50000);  
  57.         }  
  58.     } completionBlock:^{  
  59.         [HUD removeFromSuperview];  
  60.         [HUD release];  
  61.         HUD = nil;  
  62.     }];  
  63. }  
  64.   
  65. - (IBAction)showCustomDialog:(id)sender {  
  66.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  67.     [self.view addSubview:HUD];  
  68.     HUD.labelText = @"操作成功";  
  69.     HUD.mode = MBProgressHUDModeCustomView;  
  70.     HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];  
  71.     [HUD showAnimated:YES whileExecutingBlock:^{  
  72.         sleep(2);  
  73.     } completionBlock:^{  
  74.         [HUD removeFromSuperview];  
  75.         [HUD release];  
  76.         HUD = nil;  
  77.     }];  
  78.       
  79. }  
  80.   
  81. - (IBAction)showAllTextDialog:(id)sender {  
  82.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  83.     [self.view addSubview:HUD];  
  84.     HUD.labelText = @"操作成功";  
  85.     HUD.mode = MBProgressHUDModeText;  
  86.       
  87.     //指定距離中心點的X軸和Y軸的偏移量,如果不指定則在屏幕中間顯示  
  88. //    HUD.yOffset = 150.0f;  
  89. //    HUD.xOffset = 100.0f;  
  90.       
  91.     [HUD showAnimated:YES whileExecutingBlock:^{  
  92.         sleep(2);  
  93.     } completionBlock:^{  
  94.         [HUD removeFromSuperview];  
  95.         [HUD release];  
  96.         HUD = nil;  
  97.     }];  
  98. }  

依次實現的效果如下:

                          


                          

下面這個效果就類似Android中的Toast:

                                                     

純爲了大家和自己學習方便,如有冒犯,請聯繫我,然後刪除~

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