Files

326 lines
11 KiB
Mathematica
Raw Permalink Normal View History

2025-08-08 11:05:33 +08:00
//
// 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 addviewwindow
+ (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 - viewhud
+ (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