123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // MBProgressHUD+RQExtension.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/23.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "MBProgressHUD+RQExtension.h"
- #import "NSError+RQExtension.h"
- @implementation MBProgressHUD (RQExtension)
- #pragma mark - Added To window
- /// 提示信息
- + (MBProgressHUD *)rq_showTips:(NSString *)tipStr{
- return [self rq_showTips:tipStr addedToView:nil];
- }
- /// 提示错误
- + (MBProgressHUD *)rq_showErrorTips:(NSError *)error{
- return [self rq_showErrorTips:error addedToView:nil];
- }
- /// 进度view
- + (MBProgressHUD *)rq_showProgressHUD:(NSString *)titleStr
- {
- return [self rq_showProgressHUD:titleStr addedToView:nil];
- }
- /// hide hud
- + (void)rq_hideHUD
- {
- [self rq_hideHUDForView:nil];
- }
- #pragma mark - Added To Special View
- /// 提示信息
- + (MBProgressHUD *)rq_showTips:(NSString *)tipStr addedToView:(UIView *)view
- {
- return [self _showHUDWithTips:tipStr isAutomaticHide:YES addedToView:view];
- }
- /// 提示错误
- + (MBProgressHUD *)rq_showErrorTips:(NSError *)error addedToView:(UIView *)view
- {
- return [self _showHUDWithTips:[self rq_tipsFromError:error] isAutomaticHide:YES addedToView:view];
- }
- /// 进度view
- + (MBProgressHUD *)rq_showProgressHUD:(NSString *)titleStr addedToView:(UIView *)view{
- return [self _showHUDWithTips:titleStr isAutomaticHide:NO addedToView:view];
- }
- // 隐藏HUD
- + (void)rq_hideHUDForView:(UIView *)view
- {
- [self hideHUDForView:[self _willShowingToViewWithSourceView:view] animated:YES];
- }
- #pragma mark - 辅助方法
- /// 获取将要显示的view
- + (UIView *)_willShowingToViewWithSourceView:(UIView *)sourceView
- {
- if (sourceView) return sourceView;
-
- sourceView = [[UIApplication sharedApplication].delegate window];
- if (!sourceView) sourceView = [[[UIApplication sharedApplication] windows] lastObject];
-
- return sourceView;
- }
- + (instancetype )_showHUDWithTips:(NSString *)tipStr isAutomaticHide:(BOOL) isAutomaticHide addedToView:(UIView *)view
- {
- view = [self _willShowingToViewWithSourceView:view];
-
- /// 也可以show之前 hid掉之前的
- [self rq_hideHUDForView:view];
-
- MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
- HUD.mode = isAutomaticHide?MBProgressHUDModeText:MBProgressHUDModeIndeterminate;
- HUD.animationType = MBProgressHUDAnimationZoom;
- HUD.label.font = isAutomaticHide?RQMediumFont(17.0f):RQMediumFont(14.0f);
- HUD.label.numberOfLines = 0;
- HUD.label.textColor = [UIColor whiteColor];
- HUD.contentColor = [UIColor whiteColor];
- HUD.label.text = tipStr;
- HUD.bezelView.layer.cornerRadius = 8.0f;
- HUD.bezelView.layer.masksToBounds = YES;
- HUD.bezelView.color = RQColorAlpha(0, 0, 0, .6f);
- HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
- HUD.minSize =isAutomaticHide?CGSizeMake([UIScreen mainScreen].bounds.size.width-96.0f, 60):CGSizeMake(120, 120);
- HUD.margin = 18.2f;
- HUD.removeFromSuperViewOnHide = YES;
- if (isAutomaticHide) [HUD hideAnimated:YES afterDelay:2.0f];
- return HUD;
- }
- #pragma mark - 辅助属性
- + (NSString *)rq_tipsFromError:(NSError *)error{
- return [NSError rq_tipsFromError:error];
- }
- + (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.label.text = text;
- // 设置图片
- hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
- // 再设置模式
- hud.mode = MBProgressHUDModeCustomView;
-
- // 隐藏时候从父控件中移除
- hud.removeFromSuperViewOnHide = YES;
-
- // 1秒之后再消失
- [hud hideAnimated:YES afterDelay:0.7];
- }
- #pragma mark 显示错误信息
- + (void)showError:(NSString *)error toView:(UIView *)view{
- [self show:error icon:@"error.png" view:view];
- }
- + (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.label.text = message;
- // 隐藏时候从父控件中移除
- hud.removeFromSuperViewOnHide = YES;
- // YES代表需要蒙版效果
- // hud.dimBackground = YES;
- return hud;
- }
- @end
|