11.MBProgressHUD长时间加载取消

网络不好的话,加载提示就会一直存在,这样用户体验不好。可以点击的时候取消提示。

增加MBProgressHUD分类

#import "MBProgressHUD+Extension.h"

#define DelayTime   1
#define APPWindow   [UIApplication sharedApplication].keyWindow

@implementation MBProgressHUD (Extension)

+ (void)showLoadingHUD:(UIView *)view{
    if (view) {
        [self hideHUD:view];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        [hud setUserInteractionEnabled:NO];
    }
}
//点击的时候,取消加载提示,但后台还是会去加载数据的
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self hide:YES];
    
}
+ (void)showLoadingHUD{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:APPWindow animated:YES];
    [hud setUserInteractionEnabled:YES];
}

+ (void)showCustomLoadingHub:(UIView *)customView withView:(UIView *)view{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithWindow:APPWindow];
    [APPWindow addSubview:hud];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = customView;
    hud.color = [UIColor clearColor];
    [hud show:YES];
    [hud setUserInteractionEnabled:NO];
    
}

+ (void)showLoadingHUDWithText:(NSString *)text{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:APPWindow animated:YES];
    [hud setUserInteractionEnabled:YES];
    hud.labelText = STRING_OR_EMPTY(text);
}

+ (void)showLoadingHUDWithText:(NSString *)text view:(UIView *)view{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [hud setUserInteractionEnabled:YES];
    hud.labelText = STRING_OR_EMPTY(text);
}

+ (void)hideHUD
{
    [MBProgressHUD hideHUDForView:APPWindow animated:YES];
}

+ (void)hideHUD:(UIView *)view
{
    [MBProgressHUD hideHUDForView:view animated:YES];
}

+ (void)showHUDWithSuccessText:(NSString *)successText{
    [self showHUDWithCustom:AlertSuccessImage withText:successText view:APPWindow];
}

+ (void)showHUDWithFailedText:(NSString *)failedText{
    [self showHUDWithCustom:AlertFailureImage withText:failedText view:APPWindow];
}

+ (void)showHUDWithAlertText:(NSString *)alertText{
    [self showHUDWithCustom:AlertWarningImage withText:alertText view:APPWindow];
}

+ (void)showHUDWithSuccessText:(NSString *)successText view:(UIView *)view
{
    if (view) {
        [self showHUDWithCustom:AlertSuccessImage withText:successText view:view];
    }
}

+ (void)showHUDWithFailedText:(NSString *)failedText view:(UIView *)view
{
    if (view) {
        [self showHUDWithCustom:AlertFailureImage withText:failedText view:view];
    }
}

+ (void)showHUDWithAlertText:(NSString *)alertText view:(UIView *)view
{
    if (view) {
        [self showHUDWithCustom:AlertWarningImage withText:alertText view:view];
    }
}

+ (void)showHUDWithCustom:(NSString *)customImageName withText:(NSString *)customText view:(UIView *)view{
    [self showHUDWithCustom:customImageName withText:customText view:view autoHide:YES];
}

+ (MBProgressHUD *)showHUDWithCustom:(NSString *)customImageName withText:(NSString *)customText view:(UIView *)view autoHide:(BOOL)autoHide {
    [self hideHUD:view];
    MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:view];
    [view addSubview:hud];
    hud.mode = MBProgressHUDModeCustomView;
    
    CGSize size = [customText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 120, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size;
    UIImage *image = [UIImage imageNamed:customImageName];
    
    CGFloat maxWidth = MAX(size.width , image.size.width);
    MBProgressHUDCoustomView *coustomView = [MBProgressHUDCoustomView loadFromNib];
    [coustomView setFrame:CGRectMake(0, 0, maxWidth + 10, image.size.height + 15 + size.height)];
    [coustomView setImageName:customImageName withPrompt:customText];
    hud.customView = coustomView;
    
    [hud show:YES];
    if (autoHide) {
        [hud hide:YES afterDelay:DelayTime];
    }
    
    return hud;
}

@end

你可能感兴趣的:(11.MBProgressHUD长时间加载取消)