MBProgressHUD 第三方庫使用
關鍵操作:
效果如下:
ViewController.h
#import <UIKit/UIKit.h>import "MBProgressHUD.h"
@interface ViewController : UITableViewController<MBProgressHUDDelegate> @property (strong, nonatomic) MBProgressHUD hud; @property (strong, nonatomic) NSArray arrMode; @property (strong, nonatomic) NSArray *arrModeName;
@end</pre>
ViewController.m
#import "ViewController.h"@interface ViewController ()
- (void)loadData;
- (void)layoutUI;
- (void)taskOfIndeterminate;
- (void)taskOfDeterminate;
- (void)taskOfDeterminateHorizontalBar;
- (void)taskOfAnnularDeterminate;
- (void)taskOfCustomView;
- (void)taskOfText;
- (void)showHUDByIndeterminate;
- (void)showHUDByDeterminate;
- (void)showHUDByDeterminateHorizontalBar;
- (void)showHUDByAnnularDeterminate;
- (void)showHUDByCustomView;
- (void)showHUDByText; @end
@implementation ViewController
(void)viewDidLoad { [super viewDidLoad];
[self loadData]; [self layoutUI]; }
(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
(void)loadData { _arrMode = @[ @"MBProgressHUDModeIndeterminate",
@"MBProgressHUDModeDeterminate", @"MBProgressHUDModeDeterminateHorizontalBar", @"MBProgressHUDModeAnnularDeterminate", @"MBProgressHUDModeCustomView", @"MBProgressHUDModeText" ];
_arrModeName = @[ @"UIActivityIndicatorView 來顯示進度,這是默認值",
@"圓形餅圖來顯示進度", @"水平進度條來顯示進度", @"圓環來顯示進度", @"自定義視圖;例如通過這種方式,來顯示一個正確或錯誤的提示圖", @"只顯示文本" ];
}
(void)layoutUI { self.navigationItem.title = @"MBProgressHUD 第三方庫使用"; }
pragma mark - MBProgressHUD Additional Task
(void)taskOfIndeterminate { sleep(3); //進程掛起3秒,這里僅僅是模擬,相當于執行了一些操作耗時3秒;sleep 和 usleep 都是進程掛起操作方法,他們的精準度不同,所以按需使用;對于一般秒級別操作,就使用 sleep 方法 }
(void)taskOfDeterminate { CGFloat progressVal = 0.0f; while (progressVal < 1.0) {
progressVal += 0.1; _hud.progress = progressVal; usleep(500000); //千分之一毫秒,即百萬分之一秒;這里設置進程掛起0.5秒
} }
(void)taskOfDeterminateHorizontalBar { [self taskOfDeterminate]; }
(void)taskOfAnnularDeterminate { [self taskOfDeterminate]; }
(void)taskOfCustomView { [self taskOfIndeterminate]; }
(void)taskOfText { [self taskOfIndeterminate]; }
pragma mark - MBProgressHUD
(void)showHUDByIndeterminate { UIColor *color = [UIColor cyanColor];
_hud = [[MBProgressHUD alloc] initWithView:self.view]; _hud.activityIndicatorColor = color; //設置指示器顏色;默認為白色
//label 和 detailsLabel 是公共部分,其他模式的展示效果一樣可以用 _hud.labelText = @"加載中..."; _hud.labelFont = [UIFont systemFontOfSize:17]; _hud.labelColor = color; //設置文本顏色;默認為白色 _hud.detailsLabelText = @"用戶請稍候,耐心等待"; _hud.detailsLabelFont = [UIFont systemFontOfSize:14]; _hud.detailsLabelColor = color; //設置詳細文本顏色;默認為白色
//一些額外不常用的設置 _hud.minShowTime = 5.0f; _hud.opacity = 0.5f; _hud.animationType = MBProgressHUDAnimationZoomOut; _hud.cornerRadius = 15.0f; _hud.dimBackground = YES; _hud.xOffset = 0.0f; _hud.yOffset = 50.0f; _hud.margin = 30.0f; _hud.square = YES; _hud.minSize = CGSizeMake(240.0f, 200.0f); //設置了 minSize 后,square 就失效了
//設置委托,以便調用hudWasHidden:方法 _hud.delegate = self; [self.view addSubview:_hud];
//操作方式一: // [_hud showWhileExecuting:@selector(taskOfIndeterminate) // onTarget:self // withObject:nil // animated:YES];
//操作方式二: [_hud showAnimated:YES whileExecutingBlock:^{
[self taskOfIndeterminate];
} completionBlock:^{
NSLog(@"showHUDByIndeterminate 執行完成");
}]; }
(void)showHUDByDeterminate { _hud = [[MBProgressHUD alloc] initWithView:self.view]; _hud.mode = MBProgressHUDModeDeterminate; [self.view addSubview:_hud];
[_hud showAnimated:YES whileExecutingBlock:^{
[self taskOfDeterminate];
} completionBlock:^{
NSLog(@"showHUDByDeterminate 執行完成");
}]; }
(void)showHUDByDeterminateHorizontalBar { _hud = [[MBProgressHUD alloc] initWithView:self.view]; _hud.mode = MBProgressHUDModeDeterminateHorizontalBar; [self.view addSubview:_hud];
[_hud showAnimated:YES whileExecutingBlock:^{
[self taskOfDeterminateHorizontalBar];
} completionBlock:^{
NSLog(@"showHUDByDeterminateHorizontalBar 執行完成");
}]; }
(void)showHUDByAnnularDeterminate { _hud = [[MBProgressHUD alloc] initWithView:self.view]; _hud.mode = MBProgressHUDModeAnnularDeterminate; [self.view addSubview:_hud];
[_hud showAnimated:YES whileExecutingBlock:^{
[self taskOfAnnularDeterminate];
} completionBlock:^{
NSLog(@"showHUDByAnnularDeterminate 執行完成");
}]; }
(void)showHUDByCustomView { _hud = [[MBProgressHUD alloc] initWithView:self.view]; _hud.mode = MBProgressHUDModeCustomView; _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AlbumCellRedSelected"]]; [self.view addSubview:_hud];
[_hud showAnimated:YES whileExecutingBlock:^{
[self taskOfCustomView];
} completionBlock:^{
NSLog(@"showHUDByCustomView 執行完成");
}];
}
(void)showHUDByText { UIColor *color = [UIColor cyanColor];
_hud = [[MBProgressHUD alloc] initWithView:self.view]; _hud.mode = MBProgressHUDModeText; _hud.labelText = @"加載中..."; _hud.labelFont = [UIFont systemFontOfSize:17]; _hud.labelColor = color; //設置文本顏色;默認為白色 _hud.detailsLabelText = @"用戶請稍候,耐心等待"; _hud.detailsLabelFont = [UIFont systemFontOfSize:14]; _hud.detailsLabelColor = color; //設置詳細文本顏色;默認為白色 [self.view addSubview:_hud];
[_hud showAnimated:YES whileExecutingBlock:^{
[self taskOfText];
} completionBlock:^{
NSLog(@"showHUDByText 執行完成");
}]; }
pragma mark - MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud { NSLog(@"隱藏后做一些操作"); }
pragma mark - TableView
(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section { return @"展示模式列表"; }
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _arrMode.count; }
(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { static NSString cellIdentifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSInteger row = indexPath.row; cell.textLabel.text = _arrMode[row]; cell.textLabel.adjustsFontSizeToFitWidth = YES; cell.detailTextLabel.text = _arrModeName[row]; cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
return cell; }
(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath { switch (indexPath.row) {
case 0: [self showHUDByIndeterminate]; break; case 1: [self showHUDByDeterminate]; break; case 2: [self showHUDByDeterminateHorizontalBar]; break; case 3: [self showHUDByAnnularDeterminate]; break; case 4: [self showHUDByCustomView]; break; case 5: [self showHUDByText]; break;
}
NSLog(@"%ld", (long)indexPath.row); }
@end</pre>
AppDelegate.h
#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow window; @property (strong, nonatomic) UINavigationController navigationController;
@end</pre>
AppDelegate.m
#import "AppDelegate.h"import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *viewController = [[ViewController alloc] init]; _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; _window.rootViewController = _navigationController; //[_window addSubview:_navigationController.view]; //當_window.rootViewController關聯時,這一句可有可無 [_window makeKeyAndVisible]; return YES; }
(void)applicationWillResignActive:(UIApplication *)application { }
(void)applicationDidEnterBackground:(UIApplication *)application { }
(void)applicationWillEnterForeground:(UIApplication *)application { }
(void)applicationDidBecomeActive:(UIApplication *)application { }
(void)applicationWillTerminate:(UIApplication *)application { }
@end</pre>
輸出結果:
2015-07-11 13:48:30.788 MBProgressHUDDemo[7211:111189] 0 2015-07-11 13:48:36.090 MBProgressHUDDemo[7211:111189] showHUDByIndeterminate 執行完成 2015-07-11 13:48:36.091 MBProgressHUDDemo[7211:111189] 隱藏后做一些操作 2015-07-11 13:48:37.378 MBProgressHUDDemo[7211:111189] 1 2015-07-11 13:48:43.208 MBProgressHUDDemo[7211:111189] showHUDByDeterminate 執行完成 2015-07-11 13:48:44.435 MBProgressHUDDemo[7211:111189] 2 2015-07-11 13:48:50.278 MBProgressHUDDemo[7211:111189] showHUDByDeterminateHorizontalBar 執行完成 2015-07-11 13:48:51.692 MBProgressHUDDemo[7211:111189] 3 2015-07-11 13:48:57.529 MBProgressHUDDemo[7211:111189] showHUDByAnnularDeterminate 執行完成 2015-07-11 13:48:58.473 MBProgressHUDDemo[7211:111189] 4 2015-07-11 13:49:01.778 MBProgressHUDDemo[7211:111189] showHUDByCustomView 執行完成 2015-07-11 13:49:02.790 MBProgressHUDDemo[7211:111189] 5 2015-07-11 13:49:06.096 MBProgressHUDDemo[7211:111189] showHUDByText 執行完成來自:http://www.cnblogs.com/huangjianwu/p/4638542.html