Files
2025-08-08 11:05:33 +08:00

326 lines
11 KiB
Objective-C
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MBManager.m
// MBProgressDemo
//
// Created by hungryBoy on 16/1/23.
// Copyright © 2016年 hungryBoy. All rights reserved.
//
#import "MBManager.h"
#import "UIView+Toast.h"
#import "XYloading.h"
#define kScreen_height [[UIScreen mainScreen] bounds].size.height
#define kWindow_W [[UIScreen mainScreen] bounds].size.width
#define kDefaultRect CGRectMake(0, 0, kWindow_W, kScreen_height)
#define kDefaultView [[UIApplication sharedApplication] keyWindow]
#define kGloomyBlackColor [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]
#define kGloomyClearCloler [UIColor colorWithRed:1 green:1 blue:1 alpha:0]
/* 默认网络提示,可在这统一修改 */
static NSString *const kLoadingMessage = @"加载中...";
/* 默认简短提示语显示的时间,在这统一修改 */
static CGFloat const kShowTime = 2.0f;
/* 手势是否可用默认yes轻触屏幕提示框隐藏 */
static BOOL isAvalibleTouch = YES;
@implementation MBManager
UIView *gloomyView;//深色背景
UIView *prestrainView;//预加载view
BOOL isShowGloomy;//是否显示深色背景
#pragma mark - 类初始化
+ (void)initialize {
if (self == [MBManager self]) {
//该方法只会走一次
[self customView];
}
}
#pragma mark - 初始化gloomyView
+(void)customView {
gloomyView = [[GloomyView alloc] initWithFrame:kDefaultRect];
gloomyView.backgroundColor = kGloomyBlackColor;
gloomyView.hidden = YES;
isShowGloomy = NO;
}
+ (void)showGloomy:(BOOL)isShow {
isShowGloomy = isShow;
}
#pragma mark - 简短提示语
+ (void) showBriefAlert:(NSString *) message inView:(UIView *) view {
[self showBriefAlert:message time:kShowTime inView:view isHerizotal:NO];
}
+ (void)showBriefAlert:(NSString *)message time:(NSInteger)showTime inView:(UIView *)view {
[self showBriefAlert:message time:showTime inView:view isHerizotal:YES];
}
+ (void)showBriefAlert:(NSString *)message time:(NSInteger)showTime inView:(UIView *)view isHerizotal:(BOOL)isHerizontal {
dispatch_async(dispatch_get_main_queue(), ^{
prestrainView = view;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view ?:kDefaultView animated:YES];
hud.detailsLabel.text = message;
hud.detailsLabel.font = [UIFont systemFontOfSize:14];
// hud.animationType = MBProgressHUDAnimationZoom;
hud.mode = MBProgressHUDModeText;
hud.margin = 10.f;
hud.removeFromSuperViewOnHide = YES;
[hud hideAnimated:NO afterDelay:showTime];
if (isHerizontal) {
hud.transform = CGAffineTransformMakeRotation(M_PI_2);
}
});
}
#pragma mark - 长时间的提示语
+ (void) showPermanentMessage:(NSString *)message inView:(UIView *) view{
dispatch_async(dispatch_get_main_queue(), ^{
prestrainView = view;
gloomyView.frame = view ? CGRectMake(0, 0, view.frame.size.width, view.frame.size.height):
kDefaultRect;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:gloomyView animated:YES];
hud.label.text = message;
[gloomyView addSubview:hud];
[self showClearGloomyView];
[hud showAnimated:YES];
});
}
#pragma mark - 网络加载提示用
+ (void) showLoadingInView:(UIView *) view{
dispatch_async(dispatch_get_main_queue(), ^{
prestrainView = view;
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:gloomyView];
hud.label.text = kLoadingMessage;
hud.label.textColor =COLOR16(0x666666);
hud.removeFromSuperViewOnHide = YES;
gloomyView.frame = view ? CGRectMake(0, 0, view.frame.size.width, view.frame.size.height):
kDefaultRect;
if (isShowGloomy) {
[self showBlackGloomyView];
}else {
[self showClearGloomyView];
}
[gloomyView addSubview:hud];
[hud showAnimated:YES];
});
}
+ (void)showWaitingWithTitle:(NSString *)title inView:(UIView *)view {
dispatch_async(dispatch_get_main_queue(), ^{
prestrainView = view;
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:gloomyView];
hud.label.text = title;
hud.removeFromSuperViewOnHide = YES;
gloomyView.frame = view ? CGRectMake(0, 0, view.frame.size.width, view.frame.size.height):
kDefaultRect;
if (isShowGloomy) {
[self showBlackGloomyView];
}else {
[self showClearGloomyView];
}
[gloomyView addSubview:hud];
[hud showAnimated:YES];
});
}
+ (void)showWaitingInView:(UIView *)view {
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hudView = [MBManager HUDForView:gloomyView];
[hudView removeFromSuperview];
hudView = nil;
prestrainView = view;
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:gloomyView];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.color = UIColor.clearColor;
hud.contentColor = [UIColor whiteColor];//将转圈和文字设置成白色
hud.removeFromSuperViewOnHide = YES;
gloomyView.frame = view ? CGRectMake(0, 0, view.frame.size.width, view.frame.size.height):
kDefaultRect;
if (isShowGloomy) {
[self showBlackGloomyView];
}else {
[self showClearGloomyView];
}
[gloomyView addSubview:hud];
[hud showAnimated:YES];
});
}
+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title inView:(UIView *)view{
dispatch_async(dispatch_get_main_queue(), ^{
prestrainView = view;
gloomyView.frame = view ? CGRectMake(0, 0, view.frame.size.width, view.frame.size.height):
kDefaultRect;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view ?:kDefaultView animated:YES];
UIImageView *littleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
littleView.image = [UIImage imageNamed:imageName];
hud.customView = littleView;
hud.removeFromSuperViewOnHide = YES;
// hud.animationType = MBProgressHUDAnimationZoom;
hud.label.text = title;
// hud.mode = MBProgressHUDModeCustomView;
[hud showAnimated:YES];
[hud hideAnimated:YES afterDelay:kShowTime];
});
}
#pragma mark - 加载在window上的提示框
+(void)showLoading {
[self showLoadingInView:nil];
// [XYloading showWithStrings:@""];
}
+ (void)showWaitingWithTitle:(NSString *)title {
[self showWaitingWithTitle:title inView:nil];
}
+(void)showBriefAlert:(NSString *)alert {
//[self showBriefAlert:alert inView:nil];
[[self currentViewController].view hideAllToasts];
if ([[self currentViewController] isKindOfClass:NSClassFromString(@"SPAlertController")] || [[self currentViewController] isKindOfClass:NSClassFromString(@"UIAlertController")]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[self currentViewController].view makeToast:alert duration:1 position:CSToastPositionCenter];
});
} else {
[[self currentViewController].view makeToast:alert duration:1 position:CSToastPositionCenter];
}
}
+(void)showPermanentAlert:(NSString *)alert{
[self showPermanentMessage:alert inView:nil];
}
+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title {
[self showAlertWithCustomImage:imageName title:title inView:nil];
}
+ (void)showBriefAlert:(NSString *)message time:(NSInteger)showTime {
[self showBriefAlert:message time:showTime inView:nil isHerizotal:YES];
}
#pragma mark - GloomyView背景色
+ (void)showBlackGloomyView {
gloomyView.backgroundColor = kGloomyBlackColor;
[self gloomyConfig];
}
+ (void)showClearGloomyView {
gloomyView.backgroundColor = kGloomyClearCloler;
dispatch_async(dispatch_get_main_queue(), ^{
[self gloomyConfig];
});
}
#pragma mark - 决定GloomyView add到已给view或者window上
+ (void)gloomyConfig {
gloomyView.hidden = NO;
gloomyView.alpha = 1;
if (prestrainView) {
[prestrainView addSubview:gloomyView];
}else {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (![window.subviews containsObject:gloomyView]) {
[window addSubview:gloomyView];
}
}
}
#pragma mark - 隐藏提示框
+(void)hideAlert{
dispatch_async(dispatch_get_main_queue(), ^{
[XYloading dismiss];
MBProgressHUD *hud = [MBManager HUDForView:gloomyView];
#if 1
gloomyView.frame = CGRectZero;
gloomyView.center = prestrainView ? prestrainView.center: [UIApplication sharedApplication].keyWindow.center;
gloomyView.alpha = 0;
[hud removeFromSuperview];
[gloomyView removeFromSuperview];
#else
[UIView animateWithDuration:0.5 animations:^{
gloomyView.frame = CGRectZero;
gloomyView.center = prestrainView ? prestrainView.center: [UIApplication sharedApplication].keyWindow.center;
gloomyView.alpha = 0;
hud.alpha = 0;
} completion:^(BOOL finished) {
[hud removeFromSuperview];
}];
#endif
});
}
+ (void)showProgress:(float)progress
{
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:gloomyView];
//MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view ?:kDefaultView animated:YES];
// hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
// hud.bezelView.color = kClearColor;
// hud.contentColor = [UIColor whiteColor];//将转圈和文字设置成白色
hud.removeFromSuperViewOnHide = YES;
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.progress = progress;
[hud showAnimated:YES];
}
#pragma mark - 获取view上的hud
+ (MBProgressHUD *)HUDForView:(UIView *)view {
NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
for (UIView *subview in subviewsEnum) {
if ([subview isKindOfClass:[MBProgressHUD class]]) {
return (MBProgressHUD *)subview;
}
}
return nil;
}
+ (UIView *)gloomyViewForManager {
if (gloomyView) {
return gloomyView;
}
return nil;
}
////获取到根视图控制器
+ (UIViewController *)currentViewController {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// modal展现方式的底层视图不同
UIViewController *rootViewController = keyWindow.rootViewController;
UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
// if ([currentVC isKindOfClass:NSClassFromString(@"SPAlertController")]) {
// return
// }
return currentVC;
}
+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC {
UIViewController *currentVC;
if ([rootVC presentedViewController]) {
// 视图是被presented出来的
rootVC = [rootVC presentedViewController];
}
if ([rootVC isKindOfClass:[UITabBarController class]]) {
// 根视图为UITabBarController
currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
} else if ([rootVC isKindOfClass:[UINavigationController class]]) {
// 根视图为UINavigationController
currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
} else {
// 根视图为非导航类
currentVC = rootVC;
}
return currentVC;
}
@end
@implementation GloomyView
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if (isAvalibleTouch) {
[MBManager hideAlert];
}
}
@end