KSHudLoagingManager.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // KSHudLoagingManager.m
  3. // GuanYueTeam
  4. //
  5. // Created by 王智 on 2022/11/14.
  6. //
  7. #import "KSHudLoagingManager.h"
  8. #import <KSToolLibrary/UIView+Hints.h>
  9. #define PROMPT_TIME 1.5f
  10. @interface KSHudLoagingManager ()
  11. @end
  12. @implementation KSHudLoagingManager
  13. + (instancetype)shareInstance {
  14. static KSHudLoagingManager *manager = nil;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. manager = [[KSHudLoagingManager alloc] init];
  18. });
  19. return manager;
  20. }
  21. - (void)showCustomLoading:(NSString *)text {
  22. dispatch_main_async_safe(^{
  23. self.loadingView.showCancelButton = NO;
  24. self.loadingView.displayText = text;
  25. [self.loadingView showLoadingView];
  26. });
  27. }
  28. - (void)showCancelCustomLoading:(NSString *)text cancel:(KSLoadingCancel)cancel {
  29. dispatch_main_async_safe(^{
  30. self.loadingView.showCancelButton = YES;
  31. self.loadingView.displayText = text;
  32. [self.loadingView showLoadingView];
  33. [self.loadingView cancelActionCallback:^{
  34. cancel();
  35. }];
  36. });
  37. }
  38. - (void)removeCustomLoading {
  39. dispatch_main_async_safe(^{
  40. if (self->_loadingView) {
  41. [self.loadingView hideLoadingView];
  42. self->_loadingView = nil;
  43. }
  44. });
  45. }
  46. - (void)showHUD {
  47. dispatch_main_async_safe(^{
  48. [self removeLoadingView];
  49. UIWindow *window = [NSObject getKeyWindow];
  50. self.HUD = [window addHUDActivityViewToView:nil
  51. HintsText:@"加载中..."
  52. Image:nil
  53. hideAfterDelay:15.0f
  54. HaveDim:NO];
  55. self.HUD.mode = MBProgressHUDModeIndeterminate;//显示菊花
  56. [window bringSubviewToFront:self.HUD];
  57. });
  58. }
  59. - (void)removeHUD {
  60. dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC), ^{
  61. [self removeLoadingView];
  62. });
  63. }
  64. - (void)removeHUDNoDelay {
  65. [self removeLoadingView];
  66. }
  67. - (void)removeLoadingView {
  68. dispatch_main_async_safe(^{
  69. if (self.HUD) {
  70. [self.HUD removeFromSuperview];
  71. self->_HUD = nil;
  72. }
  73. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  74. });
  75. }
  76. - (void)MBPShow:(NSString *)str inView:(UIView *)displayView {
  77. [self showHud:str inView:displayView autoHide:YES];
  78. }
  79. - (void)showHud:(NSString *)str inView:(UIView *)displayView autoHide:(BOOL)autoHide {
  80. dispatch_main_async_safe(^{
  81. [self removeLoadingView];
  82. self.HUD = [MBProgressHUD showHUDAddedTo:displayView animated:YES];
  83. self.HUD.removeFromSuperViewOnHide =YES;
  84. self.HUD.mode = MBProgressHUDModeText;
  85. self.HUD.label.attributedText = [self getAttrStringWithText:str];
  86. self.HUD.label.numberOfLines = 0;
  87. self.HUD.minSize = CGSizeMake(132.f, 30.0f);
  88. self.HUD.margin = 10.0;
  89. self.HUD.label.textColor = [UIColor whiteColor];
  90. self.HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  91. self.HUD.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8f);
  92. if (autoHide) {
  93. double hiddenTime = str.length > 15 ? 3.0f : 1.5f;
  94. [self.HUD hideAnimated:YES afterDelay:hiddenTime];
  95. }
  96. });
  97. }
  98. - (void)MBShowInWindow:(NSString *)str {
  99. if ([NSString isEmptyString:str]) {
  100. return;
  101. }
  102. [self showHud:str inView:[NSObject getKeyWindow] autoHide:NO];
  103. }
  104. - (void)MBShowAUTOHidingInWindow:(NSString *)str {
  105. if ([NSString isEmptyString:str]) {
  106. return;
  107. }
  108. [self showHud:str inView:[NSObject getKeyWindow] autoHide:YES];
  109. }
  110. - (MBProgressHUD *)MBPShowLoadingHubWithText:(NSString *)text {
  111. [self removeLoadingView];
  112. self.HUD = [MBProgressHUD showHUDAddedTo:[NSObject getKeyWindow] animated:YES];
  113. self.HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
  114. self.HUD.label.text = text;
  115. self.HUD.contentColor = [UIColor whiteColor];
  116. self.HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  117. self.HUD.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8f);
  118. self.HUD.removeFromSuperViewOnHide = YES;
  119. self.HUD.progress = 0;
  120. return self.HUD;
  121. }
  122. - (void)KSShowMsg:(NSString *)message promptCompletion:(void (^)(void))promptCompletion {
  123. [self KSShowMsg:message inView:[NSObject getKeyWindow] promptCompletion:promptCompletion];
  124. }
  125. - (void)KSShowMsg:(NSString *)message inView:(UIView *)displayView promptCompletion:(void (^)(void))promptCompletion {
  126. [self removeLoadingView];
  127. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:displayView animated:YES];
  128. hud.removeFromSuperViewOnHide = YES;
  129. hud.mode = MBProgressHUDModeText;
  130. hud.label.attributedText = [self getAttrStringWithText:message];
  131. hud.label.numberOfLines = 0;
  132. hud.label.textColor = [UIColor whiteColor];
  133. hud.minSize = CGSizeMake(132.0f, 30.0f);
  134. hud.margin = 10.0;
  135. hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  136. hud.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8);
  137. [hud hideAnimated:YES afterDelay:PROMPT_TIME];
  138. dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(PROMPT_TIME * NSEC_PER_SEC), ^{
  139. promptCompletion();
  140. });
  141. }
  142. - (KSCustomLoadingView *)loadingView {
  143. if (!_loadingView) {
  144. _loadingView = [KSCustomLoadingView shareInstance];
  145. }
  146. return _loadingView;
  147. }
  148. - (KSProgressLoadingView *)progressLoading {
  149. if (!_progressLoading) {
  150. _progressLoading = [KSProgressLoadingView shareInstance];
  151. }
  152. return _progressLoading;
  153. }
  154. /// 加载进度loading
  155. /// - Parameters:
  156. /// - text: 文本
  157. /// - progress: 进度比例 0~1
  158. - (void)showProgressLoading:(NSString *)text progress:(CGFloat)progress {
  159. dispatch_main_async_safe(^{
  160. [self.progressLoading configProgressWithText:text progress:progress];
  161. [self.progressLoading showLoadingView];
  162. });
  163. }
  164. /// 加载进度loading
  165. /// - Parameters:
  166. /// - text: 文本
  167. /// - progress: 进度比例 0~1
  168. - (void)showProgressNoAnimationLoading:(NSString *)text progress:(CGFloat)progress {
  169. dispatch_main_async_safe(^{
  170. [self.progressLoading configProgressNoAnimationWithText:text progress:progress];
  171. [self.progressLoading showLoadingView];
  172. });
  173. }
  174. /// 加载进度loading 完成后回调
  175. /// - Parameters:
  176. /// - text: 文本
  177. /// - progress: 进度比例 0~1
  178. /// - promptCompletion: 回调
  179. - (void)showProgressLoading:(NSString *)text progress:(CGFloat)progress promptCompletion:(void(^)(void))promptCompletion {
  180. dispatch_main_async_safe(^{
  181. [self.progressLoading configProgressWithText:text progress:progress promptCompletion:^{
  182. promptCompletion();
  183. }];
  184. [self.progressLoading showLoadingView];
  185. });
  186. }
  187. /// 移除进度loading
  188. - (void)removeProgressLoading {
  189. dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC), ^{
  190. [self.progressLoading hideLoadingView];
  191. });
  192. }
  193. - (void)removeProgressLoadingNoDelay {
  194. [self.progressLoading hideLoadingView];
  195. }
  196. // 进度loading后续操作
  197. - (void)KSShowProgressMsg:(NSString *)message promptCompletion:(void(^)(void))promptCompletion {
  198. [self.progressLoading configProgressWithText:message progress:1];
  199. dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(PROMPT_TIME * NSEC_PER_SEC), ^{
  200. [self removeProgressLoadingNoDelay];
  201. promptCompletion();
  202. });
  203. }
  204. - (NSMutableAttributedString *)getAttrStringWithText:(NSString *)text {
  205. if (![NSString isEmptyString:text]) {
  206. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  207. [paragraphStyle setLineSpacing:6];//调整行间距
  208. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:HexRGB(0xffffff)}];
  209. return attr;
  210. }
  211. else {
  212. return [[NSMutableAttributedString alloc] initWithString:@""];
  213. }
  214. }
  215. @end