美文网首页
MBProgressHUD中whileExecutingBloc

MBProgressHUD中whileExecutingBloc

作者: 隔壁老鹿 | 来源:发表于2017-06-15 16:38 被阅读0次

    (转载)

    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

    @interfaceViewController()

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    [selfcreateView];//创建各种HUD效果的点击按钮

    }

    //创建各种HUD效果的点击按钮

    - (void)createView{

    NSArray*HudTypeArray =[NSArrayarrayWithObjects:@"菊花怪",@"圆饼图",@"进度条",@"圆环",@"文字",@"自定义",nil];

    for(inti=0; i

    UIButton*hudButton =[UIButtonbuttonWithType:UIButtonTypeCustom];

    hudButton.frame=CGRectMake(50, (50+20)*i+44+20,100,50);

    [hudButtonsetTitle:HudTypeArray[i]forState:UIControlStateNormal];

    [hudButtonsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];

    hudButton.backgroundColor=[UIColororangeColor];

    hudButton.tag=1000+i;

    [hudButtonaddTarget:selfaction:@selector(hudAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:hudButton];

    }

    }

    //设置hud的提示文字、样式、代理等等

    - (void)hudAction:(UIButton*)button {

    MBProgressHUD*HUD =[MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];//HUD效果添加哪个视图上

    HUD.label.text=@"正在努力加载中...";//加载时的提示文字

    HUD.detailsLabel.text=@"猪猪侠在这";//详细提示文字,跟UITableViewCell的detailTextLabel差不多

    HUD.delegate=self;//我在这里设置,只为实现hudWasHidden方法,使hud消失时能清理对象,省出内存

    switch(button.tag-1000) {

    case0:

    //菊花怪

    HUD.mode=MBProgressHUDModeIndeterminate;//加载效果的显示样式

    break;

    case1:

    //圆饼图

    HUD.mode=MBProgressHUDModeDeterminate;

    break;

    case2:

    //进度条

    HUD.mode=MBProgressHUDModeDeterminateHorizontalBar;

    break;

    case3:

    //圆环

    HUD.mode=MBProgressHUDModeAnnularDeterminate;

    break;

    case4:

    //文字

    HUD.mode=MBProgressHUDModeText;

    break;

    case5:

    //自定义

    HUD.mode=MBProgressHUDModeCustomView;

    break;

    default:

    break;

    }

    if(button.tag-1000==5) {

    [selfcustonView:HUD];//自定义HUD效果

    }else{

    [selfanimationHud: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), ^{

    floatprogress =0.0f;

    while(progress <1.0f) {

    progress +=0.01f;

    dispatch_async(dispatch_get_main_queue(), ^{

    [MBProgressHUDHUDForView:self.view].progress= progress;

    });

    usleep(50000);

    }

    dispatch_async(dispatch_get_main_queue(), ^{

    [hudhideAnimated:YES];

    });

    });

    }

    //自定义视图

    - (void)custonView:(MBProgressHUD*)hud{

    NSMutableArray*contingentArray =[NSMutableArrayarray];

    for(inti=1; i<13; i++) {

    NSString*imgName =[NSStringstringWithFormat:@"%d",i];

    UIImage*image;

    if(iOS7) {

    image =[[UIImageimageNamed:imgName]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    }else{

    image =[UIImageimageNamed:imgName];

    }

    [contingentArrayaddObject:image];

    }

    UIImageView*hudImageView =[[UIImageViewalloc]init];

    hudImageView.animationImages=contingentArray;//UIImageView的动画组

    hudImageView.animationDuration=1.0;//每次动画的执行时间

    hudImageView.animationRepeatCount=0;//设置动画次数,0表示无限

    [hudImageViewstartAnimating];//开始动画

    hud.customView= hudImageView;//自定义的视图,将会展示为HUD效果

    [hudhideAnimated:YESafterDelay:10.0f];//10s后隐藏HUD

    }

    #pragma mark -MBProgressHUDDelegate

    - (void)hudWasHidden:(MBProgressHUD*)hud {

    [hudremoveFromSuperview];

    hud =nil;

    }

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    相关文章

      网友评论

          本文标题:MBProgressHUD中whileExecutingBloc

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