MBProgressHUD简单使用

方法:创建hud工具类

+ (MBProgressHUD *)createHUD
{
    UIWindow *window;
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow *eachWindow in windows){
        if ([eachWindow isKeyWindow]) {
            window = eachWindow;
        }
    }
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:window];
    HUD.detailsLabel.font = [UIFont boldSystemFontOfSize:16];
    [window addSubview:HUD];
    [HUD showAnimated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    
    return HUD;
}

使用场景
1、简单使用

MBProgressHUD *HUD = [Utils createHUD];
HUD.mode = MBProgressHUDModeCustomView;
HUD.label.text = @"网络异常,加载失败";
                 
[HUD hideAnimated:YES afterDelay:1];

2、稍微复杂

MBProgressHUD *HUD = [Utils createHUD];
HUD.mode = MBProgressHUDModeCustomView;
HUD.customView.backgroundColor = [UIColor whiteColor];
        
HUD.label.text = @"扫一扫上面的二维码,加我为好友";
HUD.label.font = [UIFont systemFontOfSize:13];
HUD.label.textColor = [UIColor grayColor];
HUD.customView = self.myQRCodeImageView;
[HUD addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideHUD:)]];

3、上传头像时使用

- (void)updatePortrait
{
    MBProgressHUD *HUD = [Utils createHUD];
    HUD.label.text = @"正在上传头像";

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager OSCJsonManager];
    
    NSString *strUrl = [NSString stringWithFormat:@"%@%@", OSCAPI_V2_PREFIX, OSCAPI_USER_EDIT_PORTRAIT];
    
    [manager POST:strUrl
parameters:@{@"uid":@([Config getOwnID])}
constructingBodyWithBlock:^(id formData) {
        if (_image) {
            [formData appendPartWithFileData:[Utils compressImage:_image]
                                        name:@"portrait"
                                    fileName:@"img.jpg"
                                    mimeType:@"image/jpeg"];
        }
    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            _myInfo = [OSCUserItem osc_modelWithDictionary:responseObject[@"result"]];
            
            HUD.mode = MBProgressHUDModeCustomView;
            HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HUD-done"]];
            HUD.label.text = @"头像更新成功";
        } else {
            HUD.label.text = @"头像更换失败";
        }
        [HUD hideAnimated:YES afterDelay:1];
        [Config updateNewProfile:_myInfo];
        
        [self refreshHeaderView];
        [self.refreshControl endRefreshing];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        HUD.mode = MBProgressHUDModeCustomView;
        HUD.label.text = @"网络异常,头像更换失败";
        
        [HUD hideAnimated:YES afterDelay:1];
    }];
    
}

补充:最好写一个提示的单例管理类,保证App任何时候最多只有一个提示。

你可能感兴趣的:(MBProgressHUD简单使用)