iOS在路上

MBProgressHUD加载框

2019-03-13  本文已影响0人  禄子_c79b

使用

创建 MBProgressHUD的分类

.h中

#import <MBProgressHUD/MBProgressHUD.h>

@interface MBProgressHUD (PD)

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

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

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

+ (void)hideHUD;
+ (void)hideHUDForView:(UIView *)view;

@end

.m中


#import "MBProgressHUD+PD.h"

@implementation MBProgressHUD (PD)

/**
 *  =======显示信息
 *  @param text 信息内容
 *  @param icon 图标
 *  @param view 显示的视图
 */
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil)
        view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = text;

    hud.label.textColor = Color333333;
    //hud.bezelView.style = MBProgressHUDBackgroundStyleSolidCo;
    hud.label.font = [UIFont systemFontOfSize:17.0];
    hud.userInteractionEnabled= NO;

    hud.customView = [[UIImageView alloc] initWithImage:icon];  // 设置图片
    hud.bezelView.backgroundColor = mainGrayColor;    //背景颜色
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;

    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;

    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5];
}

/**
 *  =======显示 提示信息
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

/**
 *  =======显示
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

/**
 *  =======显示错误信息
 */
+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}
/**
 *  显示提示 + 菊花
 *  @param message 信息内容
 *  @return 直接返回一个MBProgressHUD, 需要手动关闭(  ?
 */
+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}

/**
 *  显示一些信息
 *  @param message 信息内容
 *  @param view    需要显示信息的视图
 *  @return 直接返回一个MBProgressHUD,需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = YES;

    return hud;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD
{
    [self hideHUDForView:nil];
}
/**
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil)
        view = [[UIApplication sharedApplication].windows lastObject];

    [self hideHUDForView:view animated:YES];
}

@end

样式选择

用法一:文字提示+菊花,延迟s后消失,再出现文字提示


 [MBProgressHUD showMessage:@"正在加载..."];

 int64_t delayInSeconds = 2.0;      // 延迟的时间

 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

 [MBProgressHUD hideHUD];
 [MBProgressHUD showError:@"加载失败,请检查网络" toView:nil];
 });

用法二:只提示文字,延迟s后消失

    [MBProgressHUD showError:@"登录失败" toView:nil];  

    //[MBProgressHUD showSuccess:@"成功登录"];   


轻量级SVProgressHUD

新建分类
添加分类方法

+(void)showYESmessage:(NSString *)mesg;
+(void)showNOmessage:(NSString *)mesg;
+(void)showStatesMsge:(NSString *)mesg;
+(void)showStatesMsgeWithTime:(NSString *)mesg;

实现


+(void)showYESmessage:(NSString *)mesg{

    [SVProgressHUD showSuccessWithStatus:mesg];
    [SVProgressHUD setForegroundColor:[UIColor blackColor]];
    [SVProgressHUD dismissWithDelay:1];      //mesg.length/3];

}

+(void)showNOmessage:(NSString *)mesg{

    //[SVProgressHUD showErrorWithStatus:mesg];  //  X + 提示

    [SVProgressHUD showInfoWithStatus:mesg];     // ! + 提示
    [SVProgressHUD setForegroundColor:[UIColor blackColor]];
    //[SVProgressHUD showImage:nil status:mesg];

    [SVProgressHUD dismissWithDelay:1];     //mesg.length/3];
}

+(void)showStatesMsge:(NSString *)mesg{
    [SVProgressHUD showWithStatus:mesg];    //转圈 + 提示   ----! 需手动隐藏
    [SVProgressHUD setForegroundColor:mainRedColor];

    //[SVProgressHUD dismissWithDelay:1];
}

+(void)showStatesMsgeWithTime:(NSString *)mesg{

    [SVProgressHUD showWithStatus:mesg];//???

    [SVProgressHUD setForegroundColor:mainRedColor];

    [SVProgressHUD dismissWithDelay:1.5];
}

自定义SVProgressHUD

// 设置最大、小显示时间
 [SVProgressHUD setMaximumDismissTimeInterval:10.];
 [SVProgressHUD setMaximumDismissTimeInterval:1.];

//设置样式:背景黑色、字体白色
   [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
//设置蒙版暗淡,不允许触屏
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//设置蒙版颜色及透明度
    [SVProgressHUD setBackgroundLayerColor:[UIColor colorWithRed:0.3 green:0.4 blue:0.5 alpha:0.2]];
// 位置 偏移
    [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, SCREEN_H/3)];

// 环线圆角(默认18pt)
    [SVProgressHUD setRingRadius:20];
// 环线厚度(默认2pt)
    [SVProgressHUD setRingThickness:2];
// 设置HUD的圆角
    [SVProgressHUD setCornerRadius:20];
// 设置字体
    [SVProgressHUD setFont: [UIFont preferredFontForTextStyle:UIFontTextStyleCallout]];
//设置HUD前景字体、背景颜色(SVProgressHUDStyleCustom的模式下有效)
    [SVProgressHUD setForegroundColor:[UIColor yellowColor]];
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
// 设置HUD的图标icon
    [SVProgressHUD setInfoImage:[UIImage imageNamed:@"ii"]];
    [SVProgressHUD showInfoWithStatus:@"密码错误"];     
...
    [SVProgressHUD setSuccessImage:[UIImage imageNamed:@"ii"]];
    [SVProgressHUD showSuccessWithStatus:@"登录成功"];

// 设置原生动画效果
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
// 设置弹出动画
    [SVProgressHUD setFadeInAnimationDuration:0.35]; //放大出现
    [SVProgressHUD setFadeOutAnimationDuration:1.5]; // 缩小消失

相关资料
https://blog.csdn.net/wujakf/article/details/70882827
https://www.jianshu.com/p/a08d4597cf24

交互式提示

    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"未检测到支付宝!" message:@"提示信息" 
    delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [aler show];

https://www.cnblogs.com/wujy/p/5572762.html

作者:Arthur凌
链接:https://www.jianshu.com/p/e4e106dfec2e
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

上一篇 下一篇

猜你喜欢

热点阅读