zoukankan      html  css  js  c++  java
  • MBProgressHUD基础用法

    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.
    }
    
  • 相关阅读:
    如何在github中的readme.md加入项目截图
    git图片无法正常显示
    PHP错误:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
    Mac安装PHP运行环境
    YII2数据库操作出现类似Database Exception – yiidbException SQLSTATE[HY000] [2002] No such file or director
    composer 安装 Yii2遇到的BUG
    js中字节B转化成KB,MB,GB
    README.md编写教程(基本语法)
    微信扫码网页登录,redirect_uri参数错误解决方法
    记录下自己亲自做的Django项目继承QQ第三方登录
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/6694171.html
Copyright © 2011-2022 走看看