转载

MBProgressHUD 第三方库使用

关键操作:

MBProgressHUD 第三方库使用  

效果如下:

MBProgressHUD 第三方库使用  

ViewController.h

1 #import <UIKit/UIKit.h> 2 #import "MBProgressHUD.h" 3  4 @interface ViewController : UITableViewController<MBProgressHUDDelegate> 5 @property (strong, nonatomic) MBProgressHUD *hud; 6 @property (strong, nonatomic) NSArray *arrMode; 7 @property (strong, nonatomic) NSArray *arrModeName; 8  9 @end

ViewController.m

  1 #import "ViewController.h"   2    3 @interface ViewController ()   4 - (void)loadData;   5 - (void)layoutUI;   6 - (void)showHUDByIndeterminate;   7 - (void)showHUDByDeterminate;   8 - (void)showHUDByDeterminateHorizontalBar;   9 - (void)showHUDByAnnularDeterminate;  10 - (void)showHUDByCustomView;  11 - (void)showHUDByText;  12 - (void)taskOfIndeterminate;  13 - (void)taskOfDeterminate;  14 - (void)taskOfDeterminateHorizontalBar;  15 - (void)taskOfAnnularDeterminate;  16 - (void)taskOfCustomView;  17 - (void)taskOfText;  18 @end  19   20 @implementation ViewController  21   22 - (void)viewDidLoad {  23     [super viewDidLoad];  24       25     [self loadData];  26     [self layoutUI];  27 }  28   29 - (void)didReceiveMemoryWarning {  30     [super didReceiveMemoryWarning];  31     // Dispose of any resources that can be recreated.  32 }  33   34 - (void)loadData {  35     _arrMode = @[ @"MBProgressHUDModeIndeterminate",  36                   @"MBProgressHUDModeDeterminate",  37                   @"MBProgressHUDModeDeterminateHorizontalBar",  38                   @"MBProgressHUDModeAnnularDeterminate",  39                   @"MBProgressHUDModeCustomView",  40                   @"MBProgressHUDModeText" ];  41       42     _arrModeName = @[ @"UIActivityIndicatorView 来显示进度,这是默认值",  43                       @"圆形饼图来显示进度",  44                       @"水平进度条来显示进度",  45                       @"圆环来显示进度",  46                       @"自定义视图;例如通过这种方式,来显示一个正确或错误的提示图",  47                       @"只显示文本" ];  48 }  49   50 - (void)layoutUI {  51     self.navigationItem.title = @"MBProgressHUD 第三方库使用";  52 }  53   54 #pragma mark - MBProgressHUD Additional Task  55 - (void)taskOfIndeterminate {  56     sleep(3); //线程挂起3秒,这里仅仅是模拟,相当于执行了一些操作耗时3秒  57 }  58   59 - (void)taskOfDeterminate {  60     CGFloat progressVal = 0.0f;  61     while (progressVal < 1.0) {  62         progressVal += 0.1;  63         _hud.progress = progressVal;  64         usleep(500000); //千分之一毫秒,即百万分之一秒;这里设置进程挂起0.5秒  65     }  66 }  67   68 - (void)taskOfDeterminateHorizontalBar {  69     [self taskOfDeterminate];  70 }  71   72 - (void)taskOfAnnularDeterminate {  73     [self taskOfDeterminate];  74 }  75   76 - (void)taskOfCustomView {  77     [self taskOfIndeterminate];  78 }  79   80 - (void)taskOfText {  81     [self taskOfIndeterminate];  82 }  83   84 #pragma mark - MBProgressHUD  85 - (void)showHUDByIndeterminate {  86     UIColor *color = [UIColor cyanColor];  87       88     _hud = [[MBProgressHUD alloc] initWithView:self.view];  89     _hud.activityIndicatorColor = color; //设置指示器颜色;默认为白色  90       91     //label 和 detailsLabel 是公共部分,其他模式的展示效果一样可以用  92     _hud.labelText = @"加载中...";  93     _hud.labelFont = [UIFont systemFontOfSize:17];  94     _hud.labelColor = color; //设置文本颜色;默认为白色  95     _hud.detailsLabelText = @"用户请稍候,耐心等待";  96     _hud.detailsLabelFont = [UIFont systemFontOfSize:14];  97     _hud.detailsLabelColor = color; //设置详细文本颜色;默认为白色  98       99     //一些额外不常用的设置 100     _hud.minShowTime = 5.0f; 101     _hud.opacity = 0.5f; 102     _hud.animationType = MBProgressHUDAnimationZoomOut; 103     _hud.cornerRadius = 15.0f; 104     _hud.dimBackground = YES; 105     _hud.xOffset = 0.0f; 106     _hud.yOffset = 50.0f; 107     _hud.margin = 30.0f; 108     _hud.square = YES; 109     _hud.minSize = CGSizeMake(240.0f, 200.0f); //设置了 minSize 后,square 就失效了 110      111     //设置委托,以便调用hudWasHidden:方法 112     _hud.delegate = self; 113     [self.view addSubview:_hud]; 114      115     //操作方式一: 116     //    [_hud showWhileExecuting:@selector(taskOfIndeterminate) 117     //                    onTarget:self 118     //                  withObject:nil 119     //                    animated:YES]; 120      121     //操作方式二: 122     [_hud showAnimated:YES 123    whileExecutingBlock:^{ 124        [self taskOfIndeterminate]; 125    } completionBlock:^{ 126        NSLog(@"showHUDByIndeterminate 执行完成"); 127    }]; 128 } 129  130 - (void)showHUDByDeterminate { 131     _hud = [[MBProgressHUD alloc] initWithView:self.view]; 132     _hud.mode = MBProgressHUDModeDeterminate; 133     [self.view addSubview:_hud]; 134      135     [_hud showAnimated:YES 136    whileExecutingBlock:^{ 137        [self taskOfDeterminate]; 138    } completionBlock:^{ 139        NSLog(@"showHUDByDeterminate 执行完成"); 140    }]; 141 } 142  143 - (void)showHUDByDeterminateHorizontalBar { 144     _hud = [[MBProgressHUD alloc] initWithView:self.view]; 145     _hud.mode = MBProgressHUDModeDeterminateHorizontalBar; 146     [self.view addSubview:_hud]; 147      148     [_hud showAnimated:YES 149    whileExecutingBlock:^{ 150        [self taskOfDeterminateHorizontalBar]; 151    } completionBlock:^{ 152        NSLog(@"showHUDByDeterminateHorizontalBar 执行完成"); 153    }]; 154 } 155  156 - (void)showHUDByAnnularDeterminate { 157     _hud = [[MBProgressHUD alloc] initWithView:self.view]; 158     _hud.mode = MBProgressHUDModeAnnularDeterminate; 159     [self.view addSubview:_hud]; 160      161     [_hud showAnimated:YES 162    whileExecutingBlock:^{ 163        [self taskOfAnnularDeterminate]; 164    } completionBlock:^{ 165        NSLog(@"showHUDByAnnularDeterminate 执行完成"); 166    }]; 167 } 168  169 - (void)showHUDByCustomView { 170     _hud = [[MBProgressHUD alloc] initWithView:self.view]; 171     _hud.mode = MBProgressHUDModeCustomView; 172     _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AlbumCellRedSelected"]]; 173     [self.view addSubview:_hud]; 174      175     [_hud showAnimated:YES 176    whileExecutingBlock:^{ 177        [self taskOfCustomView]; 178    } completionBlock:^{ 179        NSLog(@"showHUDByCustomView 执行完成"); 180    }]; 181  182 } 183  184 - (void)showHUDByText { 185     UIColor *color = [UIColor cyanColor]; 186      187     _hud = [[MBProgressHUD alloc] initWithView:self.view]; 188     _hud.mode = MBProgressHUDModeText; 189     _hud.labelText = @"加载中..."; 190     _hud.labelFont = [UIFont systemFontOfSize:17]; 191     _hud.labelColor = color; //设置文本颜色;默认为白色 192     _hud.detailsLabelText = @"用户请稍候,耐心等待"; 193     _hud.detailsLabelFont = [UIFont systemFontOfSize:14]; 194     _hud.detailsLabelColor = color; //设置详细文本颜色;默认为白色 195     [self.view addSubview:_hud]; 196  197     [_hud showAnimated:YES 198    whileExecutingBlock:^{ 199        [self taskOfText]; 200    } completionBlock:^{ 201        NSLog(@"showHUDByText 执行完成"); 202    }]; 203 } 204  205 #pragma mark - MBProgressHUDDelegate 206 - (void)hudWasHidden:(MBProgressHUD *)hud { 207     NSLog(@"隐藏后做一些操作"); 208 } 209  210 #pragma mark - TableView 211 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 212     return @"展示模式列表"; 213 } 214  215 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 216     return 1; 217 } 218  219 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 220     return _arrMode.count; 221 } 222  223 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 224     static NSString *cellIdentifier = @"cellIdentifier"; 225     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 226     if (!cell) { 227         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 228     } 229      230     NSInteger row = indexPath.row; 231     cell.textLabel.text = _arrMode[row]; 232     cell.textLabel.adjustsFontSizeToFitWidth = YES; 233     cell.detailTextLabel.text = _arrModeName[row]; 234     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES; 235      236     return cell; 237 } 238  239 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 240     switch (indexPath.row) { 241         case 0: 242             [self showHUDByIndeterminate]; 243             break; 244         case 1: 245             [self showHUDByDeterminate]; 246             break; 247         case 2: 248             [self showHUDByDeterminateHorizontalBar]; 249             break; 250         case 3: 251             [self showHUDByAnnularDeterminate]; 252             break; 253         case 4: 254             [self showHUDByCustomView]; 255             break; 256         case 5: 257             [self showHUDByText]; 258             break; 259     } 260      261     NSLog(@"%ld", (long)indexPath.row); 262 } 263  264 @end

AppDelegate.h

1 #import <UIKit/UIKit.h> 2  3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4  5 @property (strong, nonatomic) UIWindow *window; 6 @property (strong, nonatomic) UINavigationController *navigationController; 7  8 @end

AppDelegate.m

 1 #import "AppDelegate.h"  2 #import "ViewController.h"  3   4 @interface AppDelegate ()  5   6 @end  7   8 @implementation AppDelegate  9  10  11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 12     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 13     ViewController *viewController = [[ViewController alloc] init]; 14     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 15     _window.rootViewController = _navigationController; 16     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 17     [_window makeKeyAndVisible]; 18     return YES; 19 } 20  21 - (void)applicationWillResignActive:(UIApplication *)application { 22 } 23  24 - (void)applicationDidEnterBackground:(UIApplication *)application { 25 } 26  27 - (void)applicationWillEnterForeground:(UIApplication *)application { 28 } 29  30 - (void)applicationDidBecomeActive:(UIApplication *)application { 31 } 32  33 - (void)applicationWillTerminate:(UIApplication *)application { 34 } 35  36 @end

输出结果:

 1 2015-07-11 13:48:30.788 MBProgressHUDDemo[7211:111189] 0  2 2015-07-11 13:48:36.090 MBProgressHUDDemo[7211:111189] showHUDByIndeterminate 执行完成  3 2015-07-11 13:48:36.091 MBProgressHUDDemo[7211:111189] 隐藏后做一些操作  4 2015-07-11 13:48:37.378 MBProgressHUDDemo[7211:111189] 1  5 2015-07-11 13:48:43.208 MBProgressHUDDemo[7211:111189] showHUDByDeterminate 执行完成  6 2015-07-11 13:48:44.435 MBProgressHUDDemo[7211:111189] 2  7 2015-07-11 13:48:50.278 MBProgressHUDDemo[7211:111189] showHUDByDeterminateHorizontalBar 执行完成  8 2015-07-11 13:48:51.692 MBProgressHUDDemo[7211:111189] 3  9 2015-07-11 13:48:57.529 MBProgressHUDDemo[7211:111189] showHUDByAnnularDeterminate 执行完成 10 2015-07-11 13:48:58.473 MBProgressHUDDemo[7211:111189] 4 11 2015-07-11 13:49:01.778 MBProgressHUDDemo[7211:111189] showHUDByCustomView 执行完成 12 2015-07-11 13:49:02.790 MBProgressHUDDemo[7211:111189] 5 13 2015-07-11 13:49:06.096 MBProgressHUDDemo[7211:111189] showHUDByText 执行完成
正文到此结束
Loading...