MBProgressHUD+Addtions.m 5.73 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
//
//  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)
+ (MBProgressHUD *)Javen_showSuccess:(NSString *)success onView:(UIView *)view delay:(NSTimeInterval)time complete:(void (^)(void))complete {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:hud];

    UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.tintColor = [UIColor whiteColor];
    hud.customView = imageView;
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text = success;
    [hud showAnimated:YES];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [hud removeFromSuperview];

        if (complete) {
            complete();
        }
    });
    return hud;
}

+ (void)Javen_showError:(NSString *)error onView:(UIView *)view delay:(NSTimeInterval)time complete:(void (^)(void))complete {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:hud];

    UIImage *image = [[UIImage imageNamed:@"cross"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.tintColor = [UIColor whiteColor];
    hud.customView = imageView;
    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text = error;
    [hud showAnimated:YES];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud removeFromSuperview];

        if (complete) {
            complete();
        }
    });
}

+ (MBProgressHUD *)JVShowMessage:(NSString *)message onView:(UIView *)view autoDisppear:(BOOL)autoDisppear {
    if (message.length < 1 || message == nil) {
        return nil;
    }
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    [view addSubview:hud];
    [hud showAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud removeFromSuperview];
    });
    return hud;
}

//创建一个hud 配置hud样式
+ (MBProgressHUD *)j_hudWithView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.contentColor = kMainColor;
    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 {
    [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:[UIApplication sharedApplication].keyWindow];
    hud.mode = MBProgressHUDModeCustomView;
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.tintColor = kMainColor;
    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:[UIApplication sharedApplication].keyWindow];
    [hud showAnimated:YES];
}

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

/**
 *  仅文字提示  自动消失
 *
 *  @param text 提示文字
 */
+ (void)j_textOnly:(NSString *)text {
    [self j_removeAllHud];
    MBProgressHUD *hud = [self j_hudWithView:[UIApplication sharedApplication].keyWindow];
    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];
    });
}


/**
 *  隐藏[UIApplication sharedApplication].keyWindow上面的所有hud
 */
+ (void)j_hideLoadingView {
    MBProgressHUD *hud;
    for (id view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[MBProgressHUD class]]) {
            hud = view;
            [hud hideAnimated:YES];
            hud = nil;
        }
    }
}

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

@end