IOS學習筆記29—提示框第三方庫之MBProgressHUD

來源:互聯網
上載者:User

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

                                                                

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

                                                       
                

接下來直接上代碼了,標頭檔部分:

#import <UIKit/UIKit.h>#import "MBProgressHUD.h"@interface ViewController : UIViewController{    //HUD(Head-Up Display,意思是抬頭顯示的意思)    MBProgressHUD *HUD;}- (IBAction)showTextDialog:(id)sender;- (IBAction)showProgressDialog:(id)sender;- (IBAction)showProgressDialog2:(id)sender;- (IBAction)showCustomDialog:(id)sender;- (IBAction)showAllTextDialog:(id)sender;@end

實現檔案(按鈕實現部分):

- (IBAction)showTextDialog:(id)sender {    //初始化進度框,置於當前的View當中    HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];        //如果設定此屬性則當前的view置於後台    HUD.dimBackground = YES;        //設定對話方塊文字    HUD.labelText = @"請稍等";        //顯示對話方塊    [HUD showAnimated:YES whileExecutingBlock:^{        //對話方塊顯示時需要執行的操作        sleep(3);    } completionBlock:^{        //操作執行完後取消對話方塊        [HUD removeFromSuperview];        [HUD release];        HUD = nil;    }];}- (IBAction)showProgressDialog:(id)sender {    HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];    HUD.labelText = @"正在載入";        //設定模式為進度框形的    HUD.mode = MBProgressHUDModeDeterminate;    [HUD showAnimated:YES whileExecutingBlock:^{        float progress = 0.0f;        while (progress < 1.0f) {            progress += 0.01f;            HUD.progress = progress;            usleep(50000);        }    } completionBlock:^{        [HUD removeFromSuperview];        [HUD release];        HUD = nil;    }];}- (IBAction)showProgressDialog2:(id)sender {    HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];    HUD.labelText = @"正在載入";    HUD.mode = MBProgressHUDModeAnnularDeterminate;        [HUD showAnimated:YES whileExecutingBlock:^{        float progress = 0.0f;        while (progress < 1.0f) {            progress += 0.01f;            HUD.progress = progress;            usleep(50000);        }    } completionBlock:^{        [HUD removeFromSuperview];        [HUD release];        HUD = nil;    }];}- (IBAction)showCustomDialog:(id)sender {    HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];    HUD.labelText = @"操作成功";    HUD.mode = MBProgressHUDModeCustomView;    HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];    [HUD showAnimated:YES whileExecutingBlock:^{        sleep(2);    } completionBlock:^{        [HUD removeFromSuperview];        [HUD release];        HUD = nil;    }];    }- (IBAction)showAllTextDialog:(id)sender {    HUD = [[MBProgressHUD alloc] initWithView:self.view];    [self.view addSubview:HUD];    HUD.labelText = @"操作成功";    HUD.mode = MBProgressHUDModeText;        //指定距離中心點的X軸和Y軸的位移量,如果不指定則在螢幕中間顯示//    HUD.yOffset = 150.0f;//    HUD.xOffset = 100.0f;        [HUD showAnimated:YES whileExecutingBlock:^{        sleep(2);    } completionBlock:^{        [HUD removeFromSuperview];        [HUD release];        HUD = nil;    }];}

依次實現的效果如下:

                          


                          

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

                                                     

以上就簡單介紹了MBProgressHUD的使用,這裡都是採用block的形式來操作的,這樣寫起代碼來更直觀也更高效。

加入我們的QQ群或公眾帳號請查看:Ryan's
zone公眾帳號及QQ群

歡迎關注我的新浪微博和我交流:@唐韌_Ryan

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.