美文网首页iOS那点事儿
iOS - 提示框封装

iOS - 提示框封装

作者: 路飞_Luck | 来源:发表于2018-08-01 08:54 被阅读203次

序言

在项目中,我们要给一些用户的友好提示,比如正确提示,错误提示,警告提示,正在加载中的提示,所以封装好一个信息提示的工具类可以起到事半功倍的效果。

效果如下所示
showMessage.gif

解析

该 工具类主要是基于MBProgressHUD 的基础上进行二次封装,对外提供了很多方法,外界直接根据需要,调用对应的方法即可,方便实用。

对外接口
/**
 备注:
 1. 调用 ok,error,warning 方法时,进行信息提示,有对应图片,当时间到后,会自动消失,并且不管是否有传 toView 参数,在显示的同时还是可以进行交互的.即可以重复点击
 2. 调用 message 方法,只进行信息展示,没有对应图片.当时间到后,会自动消失,并且不管是否有传 toView 参数,在显示的同时还是可以进行交互的.即可以重复点击
 3. 调用 info 方法,显示一个 loading 视图,不会自动消失,需要外界调用对应方法让其消失,并且在展示的时候禁止用户进行其他交互.所以这里添加的图层需要注意,根据需求添加图层
 */
@interface AlertUtils : NSObject
// 成功提示 - 会自动消失
+ (void)success:(NSString *)msg;

+ (void)success:(NSString *)msg duration:(int)duration;

+ (void)success:(NSString *)msg duration:(int)duration toView:(UIView *)toView;

// 错误提示 - 会自动消失
+ (void)error:(NSString *)msg;

+ (void)error:(NSString *)msg duration:(int)duration;

+ (void)error:(NSString *)msg duration:(int)duration toView:(UIView *)toView;

// 警告提示 - 会自动消失
+ (void)warning:(NSString *)msg;

+ (void)warning:(NSString *)msg duration:(int)duration;

+ (void)warning:(NSString *)msg duration:(int)duration toView:(UIView *)toView;

// 信息提示,没有图片
+ (void)message:(NSString *)msg;

+ (void)message:(NSString *)msg duration:(int)duration;

+ (void)message:(NSString *)msg duration:(int)duration toView:(UIView *)toView;

// 转菊花的提示 - 不会自动消失
+ (MBProgressHUD *)info:(NSString *)msg;

+ (MBProgressHUD *)info:(NSString *)msg toView:(UIView *)toView;

+ (MBProgressHUD *)showTopWinMessage:(NSString *)msg duration:(int)duration;
内部核心实现方法
// 会自动消失
+ (void)bgShowMessage:(NSString *)msg duration:(int)dura toView:(UIView *)toView icon:(NSString *)icon {
    if (![msg isKindOfClass:[NSString class]] || msg == nil || msg.length == 0) {
        return;
    }
    if (![NSThread isMainThread]) {
        return;
    }
    if (dura == 0) {
        dura = msg.length / kReadEverySecond;
    }
    // 设置停留时间
    dura = dura < 1.5 ? 1.5 : dura;
    dura = dura > 5 ? 5 : dura;
    
    MBProgressHUD *hud;
    if (toView == nil) {
        AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
        hud = [MBProgressHUD showHUDAddedTo:appDelegate.window animated:YES];
    } else {
        hud = [MBProgressHUD showHUDAddedTo:toView animated:YES];
    }
    
    hud.margin = 15;
    hud.label.numberOfLines = 0;
    hud.label.text = msg;
    hud.label.font = [UIFont systemFontOfSize:14];
    if (icon != nil) {
        NSString *iconName = [NSString stringWithFormat:@"MBProgressHUD.bundle/%@",icon];
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
        hud.mode = MBProgressHUDModeCustomView;
    } else {
        hud.mode = MBProgressHUDModeText;
    }
    hud.removeFromSuperViewOnHide = true;
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = NO;    // 即在展示的同时,用户还可以点击其他区域进行交互
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, dura * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
    });
}

如有不足,欢迎指正。生活源于创造,技术源于分享。

项目连接地址

相关文章

  • iOS - 提示框封装

    序言 在项目中,我们要给一些用户的友好提示,比如正确提示,错误提示,警告提示,正在加载中的提示,所以封装好一个信息...

  • iOS 文本提示框封装

    Github完整版Demo包括点击任意地方立即移除等 效果 简单几行代码大家自己看吧。 代码

  • iOS封装 AlertView提示框

    //// AlertViewTool.h// AlertActiionDemo//// Created by Ma...

  • iOS 干货收集《二》

    一.iOS开发 iOS提示框,为什么你应该使用 MBProgressHUD? iOS代码实践总结 Reactive...

  • iOS蓝牙原生封装

    iOS蓝牙原生封装 iOS蓝牙原生封装

  • MBProgressHUD && SVProgr

    iOS开发中-使用提示框的场景有不少,现在最常用的两种提示框 - MBProgressHUD && SVProgr...

  • Swift - UIAlertController

    添加AlertController 添加文本输入框 自动消失的提示框 UIAlertExtension: 封装 创...

  • iOS9提示框UIAlertController

    iOS中的提示框分为在中间显示中部.png 和在下方显示两种下方.png iOS8之前 中部的提示框使用的是UIA...

  • XXAlertView使用

    XXAlertView对UIAlertController封装,能够方便的实现警告框。 使用方法 弹出提示框 弹出...

  • iOS开发之AlertView提示框简易封装

    由于在平时开发过程中,经常会根据自己项目的需求进行定制一些提示框信息、广告信息或者对系统UIAlertContro...

网友评论

    本文标题:iOS - 提示框封装

    本文链接:https://www.haomeiwen.com/subject/jkuhvftx.html