iOS 第三方類庫之MBProgressHUD

來源:互聯網
上載者:User

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.

程式在執行一個長時間任務的時候(例如網路請求),可以使用該類庫在主視窗中添加一個提示框顯示進度或者相關的提示資訊。

[MBProgressHUD showHUDAddedTo:self.view animated:YES];dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{    // Do something...    dispatch_async(dispatch_get_main_queue(), ^{        [MBProgressHUD hideHUDForView:self.view animated:YES];    });});

在這個類庫中,提供了豐富的設定方法,可以構建出令人滿意的提示框,在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;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.