MBProgressHUD+Addtions.m 3.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
//
//  MBProgressHUD+Addtions.m
//  RealEstateManagement
//
//  Created by Z on 16/6/24.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "MBProgressHUD+Addtions.h"

//hud 延迟多久消失
static CGFloat const delay = 1.2;

@implementation MBProgressHUD (Addtions)

//创建一个hud 配置hud样式
+ (MBProgressHUD *)j_hudWithView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.contentColor = kMainBlueColor;
    hud.label.font = [UIFont systemFontOfSize:18];
    hud.detailsLabel.font = [UIFont systemFontOfSize:16];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

//成功的hud
+ (void)j_success:(NSString *)success complete:(void (^)(void))complete {
    [self j_customViewImageName:@"Checkmark" text:success complete:complete];
}

//失败的hud
+ (void)j_error:(NSString *)error complete:(void (^)(void))complete {
    if ([error isEqualToString:@"A connection failure occurred"]) {
        error = @"网络连接失败,请重试!";
    }
    [self j_customViewImageName:@"cross" text:error complete:complete];
}

//自定义hud图片
+ (MBProgressHUD *)j_customViewImageName:(NSString *)imageName text:(NSString *)text complete:(void (^)(void))complete{
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    hud.mode = MBProgressHUDModeCustomView;
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.tintColor = kMainBlueColor;
    hud.customView = imageView;
    hud.detailsLabel.text = text;
    [hud showAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self j_hideLoadingView];
        if (complete) {
            complete();
        }
    });
    return hud;
}

/**
 *  只有菊花
 */
+ (void)j_loading {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    [hud showAnimated:YES];
}

+ (MBProgressHUD *)j_loadingOnView:(UIView *)view {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:view];
    [hud showAnimated:YES];
    return hud;
}

/**
 *  菊花+文字
 *
 *  @param text 提示文字
 */
+ (void)j_loading:(NSString *)text {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    hud.label.text = text;
    [hud showAnimated:YES];
}

/**
 *  菊花+文字
 */
+ (MBProgressHUD *)j_loadingOnView:(UIView *)view text:(NSString *)text {
    MBProgressHUD *hud = [self j_hudWithView:view];
    hud.label.text = text;
    return hud;
}

/**
 *  仅文字提示  自动消失
 *
 *  @param text 提示文字
 */
+ (void)j_textOnly:(NSString *)text {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:kWindow];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = text;
    [hud showAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self j_hideLoadingView];
    });
}


/**
 *  隐藏kwindow上面的所有hud
 */
+ (void)j_hideLoadingView {
    [MBProgressHUD j_hideOnView:kWindow];
}

+ (void)j_hideOnView:(UIView *)view {
    MBProgressHUD *hud;
    for (id obj in view.subviews) {
        if ([obj isKindOfClass:[MBProgressHUD class]]) {
            hud = obj;
            [hud hideAnimated:YES];
            hud = nil;
        }
    }
}

/**
 *  删除所有hud
 */
+ (void)j_removeAllHud {
    for (id view in kWindow.subviews) {
        if ([view isKindOfClass:[MBProgressHUD class]]) {
            MBProgressHUD *hud = view;
            [hud removeFromSuperview];
            hud = nil;
        }
    }
}

@end