MBProgressHUD基础用法

作者: 猪猪侠在这z | 来源:发表于2016-05-12 15:40 被阅读2022次

MBProgressHUD版本号:0.9.2
以前用MBProgressHUD用得挺好的,基本上

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(void (^)())completion ;

这套方法上去就没事了,但是现在不行了,老是提示要用GCD

'showAnimated:whileExecutingBlock:completionBlock:' is deprecated: Use GCD directly.

没办法,只能去网上下载了:https://github.com/jdg/MBProgressHUD
看了一下Demo,还真改了,上代码吧。

#import "ViewController.h"
#import "MBProgressHUD.h"

#define iOS7 [[[UIDevice currentDevice] systemVersion] floatValue]>=7.0
@interface ViewController ()<MBProgressHUDDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView]; //创建各种HUD效果的点击按钮
}

//创建各种HUD效果的点击按钮
- (void)createView{

    
    NSArray *HudTypeArray =[NSArray arrayWithObjects:@"菊花怪",@"圆饼图",@"进度条",@"圆环",@"文字",@"自定义",nil];
    for (int i=0; i<HudTypeArray.count; i++) {
        UIButton *hudButton =[UIButton buttonWithType:UIButtonTypeCustom];
        hudButton.frame = CGRectMake(50, (50+20)*i+44+20, 100, 50);
        [hudButton setTitle:HudTypeArray[i] forState:UIControlStateNormal];
        [hudButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        hudButton.backgroundColor =[UIColor orangeColor];
        hudButton.tag = 1000+i;
        [hudButton addTarget:self action:@selector(hudAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:hudButton];
    }
}

//设置hud的提示文字、样式、代理等等
- (void)hudAction:(UIButton *)button {
    
    MBProgressHUD  *HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES]; //HUD效果添加哪个视图上
    HUD.label.text = @"正在努力加载中...";     //加载时的提示文字
    HUD.detailsLabel.text = @"猪猪侠在这";    //详细提示文字,跟UITableViewCell的detailTextLabel差不多
    HUD.delegate = self;        //我在这里设置,只为实现hudWasHidden方法,使hud消失时能清理对象,省出内存
    switch (button.tag-1000) {
        case 0:
            //菊花怪
            HUD.mode =MBProgressHUDModeIndeterminate;   //加载效果的显示样式
            
            break;
        case 1:
            //圆饼图
            HUD.mode = MBProgressHUDModeDeterminate;
            break;
        case 2:
            //进度条
            HUD.mode =MBProgressHUDModeDeterminateHorizontalBar;
            
            break;
        case 3:
            //圆环
            HUD.mode =MBProgressHUDModeAnnularDeterminate;

            break;
        case 4:
            //文字
            HUD.mode =MBProgressHUDModeText;

            break;
        case 5:
            //自定义
            HUD.mode =MBProgressHUDModeCustomView;

            break;
            
        default:
            break;
    }

     
    if (button.tag-1000==5) {
        [self custonView:HUD];  //自定义HUD效果
    }else{
        [self animationHud:HUD];    ////MBProgressHUD自带HUD效果
    }

}

//MBProgressHUD自带视图
- (void)animationHud:(MBProgressHUD *)hud {
    /**
     MBProgressHUD 0.92与先前某些版本很大的不同就包括:舍弃掉了某些方法:比如
     以前是这样玩的:
     
     [hud showAnimated:YES whileExecutingBlock:^{
     float progress = 0.0f;
     while (progress < 1.0f) {
     progress += 0.01f;
     hud.progress = progress;
     usleep(50000);
     }
     } completionBlock:^{
     [hud removeFromSuperview];
     hud = nil;
     }];
     现在不行了,得像下边这样玩:
                             |
                             |
                           * |  *
                            *  *
                             *
     **/

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        float progress = 0.0f;
        while (progress < 1.0f) {
            progress += 0.01f;
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD HUDForView:self.view].progress = progress;
            });
            usleep(50000);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hideAnimated:YES];
        });
    });
}
//自定义视图
- (void)custonView:(MBProgressHUD *)hud{
    NSMutableArray *contingentArray =[NSMutableArray array];
    for (int i=1; i<13; i++) {
        NSString *imgName =[NSString stringWithFormat:@"%d",i];
        UIImage *image;
        if (iOS7) {
            image =[[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        }else{
            image =[UIImage imageNamed:imgName];
        }

        [contingentArray addObject:image];
    }

    UIImageView *hudImageView =[[UIImageView alloc] init];
    hudImageView.animationImages =contingentArray;  //UIImageView的动画组
    hudImageView.animationDuration= 1.0;        //每次动画的执行时间
    hudImageView.animationRepeatCount = 0;      //设置动画次数,0表示无限
    [hudImageView startAnimating];              //开始动画
    hud.customView = hudImageView;              //自定义的视图,将会展示为HUD效果
    
    [hud hideAnimated:YES afterDelay:10.0f];     //10s后隐藏HUD

}

#pragma mark -MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud {
    [hud removeFromSuperview];
    hud = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



相关文章

网友评论

  • MakeThatChange:66不错,我说怎么了报GCD错误~ 楼主用心,👍 :kissing_closed_eyes:
  • bf7edeaa0ddd: [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; add后面这个参数,到底要加到哪个层才合适,我想让view不能点 但是导航栏可以点,因此我加到导航控制器view里,但是这样一来导航栏也不能点了,求解? 怎么才可以实现view不能点,导航栏能点
    猪猪侠在这z:@happycoder 你方便的话就把你的困扰写一个Demo,上传到百度云网盘,然后给我链接吧
    bf7edeaa0ddd:@猪猪侠在这 问题就在于,我加在self.view上,self.view还能点。当然,导航栏也能点
    猪猪侠在这z:@happycoder 你add后面的参数为view就好了(这个参数从不固定,比如你就在self.navigationController.view上面,整个界面就不能点了,包括leftButton;你添加在self.view上,除去导航栏其他地方就不能点了,如果是自定义的 view也是一样的)
  • CocoaH:自定义的的view时,imageView是不是要设置大小?因为我按照demo发现动画没出来,设置了imageView的大小,才看到
    猪猪侠在这z:@HK1218 首先声明一下,目前我并没有对MBProgressHUD的源码进行分析,因为时间不够,所以我只是做了几个测试。我测试时我的imageView动画组的图片是89×90的,就像我的代码中一样,我并没有设置frame,显示是正常的,后来我把图片的比例缩放在30%至%300这个区间内,HUD效果正常显示,唯一变的HUD视图的尺寸,并且长宽不一定相等。而你说的设置自己设置了frame才显示,可能是你动画组的图片尺寸太小或者太大,导致不显示或者无法显示。你说设置frame我也做过,设置后HUD显示的区域就是你设置的frame,所以我猜源码可能是对frame做了判断,如果frame就用frame,否则就根据图片尺寸来定义frame的尺寸

本文标题:MBProgressHUD基础用法

本文链接:https://www.haomeiwen.com/subject/xtrprttx.html