免费开源的iOS开发学习平台

MBProgressHUD:2-基本使用

本节我们通过展示MBProgressHUD的几种样式,讲解MBProgressHUD的使用方法。

准备工作

我们首先搭建一个简单的界面。在 Main.storyboard 中添加几个按钮控件,并与ViewController建立关联,创建每个按钮的点击方法。

MBProgressHUD的样式

  • MBProgressHUDModeIndeterminate**样式
- (IBAction)indeterminateMode:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.bezelView.color = [UIColor greenColor];
    hud.bezelView.alpha = 0.5;
    hud.label.text = @"绿色的bezelView";
    //显示3秒后隐藏
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
}

  • MBProgressHUDModeDeterminate样式
- (IBAction)determinateMode:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeDeterminate;
    hud.label.text = @"正在加载...";
    hud.detailsLabel.textColor = [UIColor redColor];
    hud.detailsLabel.text = @"红色的副标题";
    //显示3秒后隐藏
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
}

  • MBProgressHUDModeAnnularDeterminate样式
- (IBAction)annularDeterminateMode:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
    hud.label.text = @"正在加载...";
    hud.detailsLabel.text = @"黄色的backgroundView";
    hud.backgroundView.backgroundColor = [UIColor yellowColor];
    //显示3秒后隐藏
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
}

  • MBProgressHUDModeDeterminateHorizontalBar样式
- (IBAction)determinateHorizontalBarMode:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    hud.label.text = @"正在加载...";
    hud.detailsLabel.text = @"添加了一个按钮";
    NSProgress *progressObject = [NSProgress progressWithTotalUnitCount:100];
    hud.progressObject = progressObject;
    [hud.button setTitle:@"取消加载" forState:UIControlStateNormal];
    [hud.button addTarget:progressObject action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    hud.button.backgroundColor = [UIColor blueColor];
    //显示3秒后隐藏
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
}

  • MBProgressHUDModeCustomView样式
- (IBAction)customViewMode:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    UIImage *image = [[UIImage imageNamed:@"99logo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    imageView.image = image;
    hud.customView = imageView;
    hud.label.text = @"99iOS";
    hud.detailsLabel.text = @"苹果iOS开发进阶之路";
    [hud hideAnimated:YES afterDelay:3.f];
}