MBProgressHUD+Expand - iOS

利用 MBProgress 提供的接口方法进行了二次封装拓展,为了更好的使用,特此记录一下,方便今后开发使用带来的便捷.

当然,如有更好更优的 demo 欢迎大家分享,共同进步!

 废话不多说直接上 code:

 

首先,声明部分 (.h)

#import <MBProgressHUD/MBProgressHUD.h>

@interface MBProgressHUD (FYH)

+ (void)showError:(NSString *)error toView:(UIView *)view;
+ (void)showSuccess:(NSString *)success toView:(UIView *)view;

+ (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view;



/** Hud 可设置滞留时间*/
+ (void)showCommonHudWithAlertString:(NSString *)aString afterDelay:(int)time toView:(UIView *)view;

@end

 

其次,实现部分 (.m)

#import "MBProgressHUD+FYH.h"
#import <MBProgressHUD.h>

@implementation MBProgressHUD (FYH)

#pragma mark - 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:0.7];
}

#pragma mark - 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view {
    [self show:error icon:@"error.png" view:view];
}


+ (void)showError:(NSString *)error afterDelay:(int)time toView:(UIView *)view callback:(void (^)(BOOL, NSError *))callback {
    [self showCommonHudWithAlertString:error afterDelay:time toView:view];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(handleHideTimer:) userInfo:nil repeats:NO];//[NSTimer timerWithTimeInterval:time target:self selector:@selector(handleHideTimer:) userInfo:@(YES) repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    //    [self show:error icon:@"error.png" view:view];
}
- (void)handleHideTimer:(NSTimer *)timer {
    [self hideAnimated:[timer.userInfo boolValue]];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    [self show:success icon:@"success.png" view:view];
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = YES;
    return hud;
}

#pragma mark - 设置展示时间
+ (void)showCommonHudWithAlertString:(NSString *)aString afterDelay:(int)time toView:(UIView *)view {
    MBProgressHUD *commonHud;
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    
    if (commonHud) {
        [commonHud hideAnimated:YES];
        commonHud = nil;
    }
    commonHud = [[MBProgressHUD alloc] initWithView:view];
    commonHud.label.text = aString;
    commonHud.mode = MBProgressHUDModeText;
//    commonHud.removeFromSuperViewOnHide = YES;
    [view addSubview:commonHud];
    [view bringSubviewToFront:commonHud];
    [commonHud showAnimated:YES];
    [commonHud hideAnimated:YES afterDelay:time];
}

@end

 

 

再其次,若不想单起一个类也可以在当前调用方法之处手动添加 runloop 来实现,具体 code 实现如下:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        
    // insert your task code here
        
    [MBProgressHUD showError:@"请输入您的账号" toView:self.view];
        
});

 

最后,此次分享暂告一段落,后期仍会在持续更新中.

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值