123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // KSHudLoagingManager.m
- // GuanYueTeam
- //
- // Created by 王智 on 2022/11/14.
- //
- #import "KSHudLoagingManager.h"
- #import <KSToolLibrary/UIView+Hints.h>
- #define PROMPT_TIME 1.5f
- @interface KSHudLoagingManager ()
- @end
- @implementation KSHudLoagingManager
- + (instancetype)shareInstance {
-
- static KSHudLoagingManager *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[KSHudLoagingManager alloc] init];
- });
- return manager;
- }
- - (void)showCustomLoading:(NSString *)text {
- dispatch_main_async_safe(^{
- self.loadingView.showCancelButton = NO;
- self.loadingView.displayText = text;
- [self.loadingView showLoadingView];
- });
- }
- - (void)showCancelCustomLoading:(NSString *)text cancel:(KSLoadingCancel)cancel {
- dispatch_main_async_safe(^{
- self.loadingView.showCancelButton = YES;
- self.loadingView.displayText = text;
- [self.loadingView showLoadingView];
- [self.loadingView cancelActionCallback:^{
- cancel();
- }];
- });
- }
- - (void)removeCustomLoading {
- dispatch_main_async_safe(^{
- if (self->_loadingView) {
- [self.loadingView hideLoadingView];
- self->_loadingView = nil;
- }
- });
- }
- - (void)showHUD {
- dispatch_main_async_safe(^{
- [self removeLoadingView];
- UIWindow *window = [NSObject getKeyWindow];
- self.HUD = [window addHUDActivityViewToView:nil
- HintsText:@"加载中..."
- Image:nil
- hideAfterDelay:15.0f
- HaveDim:NO];
- self.HUD.mode = MBProgressHUDModeIndeterminate;//显示菊花
-
- [window bringSubviewToFront:self.HUD];
- });
- }
- - (void)removeHUD {
- dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC), ^{
- [self removeLoadingView];
- });
- }
- - (void)removeHUDNoDelay {
- [self removeLoadingView];
- }
- - (void)removeLoadingView {
- dispatch_main_async_safe(^{
- if (self.HUD) {
- [self.HUD removeFromSuperview];
- self->_HUD = nil;
- }
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
- });
- }
- - (void)MBPShow:(NSString *)str inView:(UIView *)displayView {
- [self showHud:str inView:displayView autoHide:YES];
- }
- - (void)showHud:(NSString *)str inView:(UIView *)displayView autoHide:(BOOL)autoHide {
- dispatch_main_async_safe(^{
- [self removeLoadingView];
- self.HUD = [MBProgressHUD showHUDAddedTo:displayView animated:YES];
- self.HUD.removeFromSuperViewOnHide =YES;
- self.HUD.mode = MBProgressHUDModeText;
- self.HUD.label.attributedText = [self getAttrStringWithText:str];
- self.HUD.label.numberOfLines = 0;
- self.HUD.minSize = CGSizeMake(132.f, 30.0f);
- self.HUD.margin = 10.0;
- self.HUD.label.textColor = [UIColor whiteColor];
- self.HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
- self.HUD.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8f);
- if (autoHide) {
- double hiddenTime = str.length > 15 ? 3.0f : 1.5f;
- [self.HUD hideAnimated:YES afterDelay:hiddenTime];
- }
- });
- }
- - (void)MBShowInWindow:(NSString *)str {
- if ([NSString isEmptyString:str]) {
- return;
- }
- [self showHud:str inView:[NSObject getKeyWindow] autoHide:NO];
- }
- - (void)MBShowAUTOHidingInWindow:(NSString *)str {
- if ([NSString isEmptyString:str]) {
- return;
- }
- [self showHud:str inView:[NSObject getKeyWindow] autoHide:YES];
- }
- - (MBProgressHUD *)MBPShowLoadingHubWithText:(NSString *)text {
- [self removeLoadingView];
- self.HUD = [MBProgressHUD showHUDAddedTo:[NSObject getKeyWindow] animated:YES];
- self.HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
- self.HUD.label.text = text;
- self.HUD.contentColor = [UIColor whiteColor];
- self.HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
- self.HUD.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8f);
- self.HUD.removeFromSuperViewOnHide = YES;
- self.HUD.progress = 0;
- return self.HUD;
- }
- - (void)KSShowMsg:(NSString *)message promptCompletion:(void (^)(void))promptCompletion {
- [self KSShowMsg:message inView:[NSObject getKeyWindow] promptCompletion:promptCompletion];
-
- }
- - (void)KSShowMsg:(NSString *)message inView:(UIView *)displayView promptCompletion:(void (^)(void))promptCompletion {
- [self removeLoadingView];
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:displayView animated:YES];
- hud.removeFromSuperViewOnHide = YES;
- hud.mode = MBProgressHUDModeText;
- hud.label.attributedText = [self getAttrStringWithText:message];
- hud.label.numberOfLines = 0;
- hud.label.textColor = [UIColor whiteColor];
- hud.minSize = CGSizeMake(132.0f, 30.0f);
- hud.margin = 10.0;
- hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
- hud.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8);
- [hud hideAnimated:YES afterDelay:PROMPT_TIME];
-
- dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(PROMPT_TIME * NSEC_PER_SEC), ^{
- promptCompletion();
- });
- }
- - (KSCustomLoadingView *)loadingView {
- if (!_loadingView) {
- _loadingView = [KSCustomLoadingView shareInstance];
- }
- return _loadingView;
- }
- - (KSProgressLoadingView *)progressLoading {
- if (!_progressLoading) {
- _progressLoading = [KSProgressLoadingView shareInstance];
- }
- return _progressLoading;
- }
- /// 加载进度loading
- /// - Parameters:
- /// - text: 文本
- /// - progress: 进度比例 0~1
- - (void)showProgressLoading:(NSString *)text progress:(CGFloat)progress {
- dispatch_main_async_safe(^{
- [self.progressLoading configProgressWithText:text progress:progress];
- [self.progressLoading showLoadingView];
- });
- }
- /// 加载进度loading
- /// - Parameters:
- /// - text: 文本
- /// - progress: 进度比例 0~1
- - (void)showProgressNoAnimationLoading:(NSString *)text progress:(CGFloat)progress {
- dispatch_main_async_safe(^{
- [self.progressLoading configProgressNoAnimationWithText:text progress:progress];
- [self.progressLoading showLoadingView];
- });
- }
- /// 加载进度loading 完成后回调
- /// - Parameters:
- /// - text: 文本
- /// - progress: 进度比例 0~1
- /// - promptCompletion: 回调
- - (void)showProgressLoading:(NSString *)text progress:(CGFloat)progress promptCompletion:(void(^)(void))promptCompletion {
- dispatch_main_async_safe(^{
- [self.progressLoading configProgressWithText:text progress:progress promptCompletion:^{
- promptCompletion();
- }];
- [self.progressLoading showLoadingView];
- });
-
- }
- /// 移除进度loading
- - (void)removeProgressLoading {
- dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC), ^{
- [self.progressLoading hideLoadingView];
- });
- }
- - (void)removeProgressLoadingNoDelay {
- [self.progressLoading hideLoadingView];
- }
- // 进度loading后续操作
- - (void)KSShowProgressMsg:(NSString *)message promptCompletion:(void(^)(void))promptCompletion {
- [self.progressLoading configProgressWithText:message progress:1];
-
- dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(PROMPT_TIME * NSEC_PER_SEC), ^{
- [self removeProgressLoadingNoDelay];
- promptCompletion();
- });
- }
- - (NSMutableAttributedString *)getAttrStringWithText:(NSString *)text {
- if (![NSString isEmptyString:text]) {
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setLineSpacing:6];//调整行间距
- NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:HexRGB(0xffffff)}];
- return attr;
- }
- else {
- return [[NSMutableAttributedString alloc] initWithString:@""];
- }
- }
- @end
|