KSHudLoagingManager.m 8.0 KB

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