iOS 第三方MBProgressHUD使用的问题

本文介绍了在iOS开发中使用MBProgressHUD时遇到需要使用GCD的情况,详细讲解了如何按照MBProgressHUD官方文档进行配置,包括设置不同模式、调整布局、动画效果、进度更新以及在GCD中正确操作MBProgressHUD的方法,确保其在隐藏时正确释放资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前用过MBProgressHUD和SVProgressHUD,两个第三方都挺好用

最近使用MBProgressHUD一直报让使用GCD

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

MBProgressHUD官网的地址: https://github.com/jdg/MBProgressHUD

可以按照官网上进行修改

-(void)createMBProgressHUD

{

    MBProgressHUD  *HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES]; //HUD的创建

    //    HUD.mode = MBProgressHUDModeIndeterminate;//菊花,默认值

    //    HUD.mode = MBProgressHUDModeDeterminate;//圆饼,饼状图

    //    HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;//进度条

    HUD.mode = MBProgressHUDModeAnnularDeterminate;//圆环作为进度条

    //    HUD.mode = MBProgressHUDModeCustomView; //需要设置自定义视图时候设置成这个

    //    HUD.mode = MBProgressHUDModeText; //只显示文本

    HUD.label.text = @"正在加载中...";     // 标题(加载时的提示文字)

    HUD.detailsLabel.text = @"一直在奔跑";    // 副标题

    HUD.delegate = self;        // 使hud消失时能清理对象,省出内存

    //1,设置各个元素距离矩形边框的距离

    HUD.margin = 0;

    //2,背景框的最小大小

    HUD.minSize = CGSizeMake(50, 50);

    //3,是否强制背景框宽高相等

    HUD.square = YES;

    //4,设置显示和隐藏动画类型  有三种动画效果,如下

//    HUD.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变

//    HUD.animationType = MBProgressHUDAnimationZoomOut; //HUD的整个view后退 然后逐渐的后退

//    HUD.animationType = MBProgressHUDAnimationZoomIn; //和上一个相反,前近,最后淡化消失

    //5,设置最短显示时间,为了避免显示后立刻被隐藏   默认是0

//        HUD.minShowTime = 10;

    //6,设置隐藏的时候是否从父视图中移除,默认是NO

    HUD.removeFromSuperViewOnHide = NO;

    //7,进度指示器  模式是0,取值从0.0————1.0

    //    HUD.progress = 0.5;

    //8,隐藏时候的回调 隐藏动画结束之后

    HUD.completionBlock = ^(){

        NSLog(@"abnnfsfsf");

    };

    

    [self animationHud:HUD];    MBProgressHUD自带HUD效果

}


//MBProgressHUD

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

    /**

     MBProgressHUD 有一些方法已经不能用了:比如

     

     [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];

        });

    });

}


#pragma mark -MBProgressHUDDelegate

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

    [hud removeFromSuperview];

    hud = nil;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值