KSHudLoagingManager.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // KSHudLoagingManager.m
  3. // GuanYueTeam
  4. //
  5. // Created by 王智 on 2022/11/14.
  6. //
  7. #import "KSHudLoagingManager.h"
  8. #import <MBProgressHUD/MBProgressHUD.h>
  9. #import "UIView+Hints.h"
  10. #define PROMPT_TIME 1.5f
  11. @interface KSHudLoagingManager ()
  12. @property (nonatomic, strong) MBProgressHUD *HUD;
  13. @end
  14. @implementation KSHudLoagingManager
  15. + (instancetype)shareInstance {
  16. static KSHudLoagingManager *manager = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. manager = [[KSHudLoagingManager alloc] init];
  20. });
  21. return manager;
  22. }
  23. - (void)showHUD {
  24. dispatch_main_async_safe(^{
  25. [self removeLoadingView];
  26. UIWindow *window = [[UIApplication sharedApplication ] keyWindow];
  27. self.HUD = [window addHUDActivityViewToView:nil
  28. HintsText:@"加载中..."
  29. Image:nil
  30. hideAfterDelay:15.0f
  31. HaveDim:NO];
  32. self.HUD.mode = MBProgressHUDModeIndeterminate;//显示菊花
  33. [window bringSubviewToFront:self.HUD];
  34. });
  35. }
  36. - (void)removeHUD {
  37. dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC), ^{
  38. [self removeLoadingView];
  39. });
  40. }
  41. - (void)removeLoadingView {
  42. if (self.HUD) {
  43. [self.HUD removeFromSuperview];
  44. }
  45. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  46. }
  47. - (void)MBPShow:(NSString *)str inView:(UIView *)displayView {
  48. [self showHud:str inView:displayView autoHide:YES];
  49. }
  50. - (void)showHud:(NSString *)str inView:(UIView *)displayView autoHide:(BOOL)autoHide {
  51. [self removeLoadingView];
  52. self.HUD = [MBProgressHUD showHUDAddedTo:displayView animated:YES];
  53. self.HUD.removeFromSuperViewOnHide =YES;
  54. self.HUD.mode = MBProgressHUDModeText;
  55. self.HUD.label.text = str;
  56. self.HUD.label.numberOfLines = 0;
  57. self.HUD.minSize = CGSizeMake(132.f, 60.0f);
  58. self.HUD.label.textColor = [UIColor whiteColor];
  59. self.HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  60. self.HUD.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8f);
  61. if (autoHide) {
  62. double hiddenTime = str.length > 15 ? 3.0f : 1.5f;
  63. [self.HUD hideAnimated:YES afterDelay:hiddenTime];
  64. }
  65. }
  66. - (void)MBShowInWindow:(NSString *)str {
  67. [self showHud:str inView:[NSObject getKeyWindow] autoHide:NO];
  68. }
  69. - (void)MBShowAUTOHidingInWindow:(NSString *)str {
  70. [self showHud:str inView:[NSObject getKeyWindow] autoHide:YES];
  71. }
  72. - (void)KSShowMsg:(NSString *)message promptCompletion:(void (^)(void))promptCompletion {
  73. [self KSShowMsg:message inView:[NSObject getKeyWindow] promptCompletion:promptCompletion];
  74. }
  75. - (void)KSShowMsg:(NSString *)message inView:(UIView *)displayView promptCompletion:(void (^)(void))promptCompletion {
  76. [self removeLoadingView];
  77. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:displayView animated:YES];
  78. hud.removeFromSuperViewOnHide = YES;
  79. hud.mode = MBProgressHUDModeText;
  80. hud.label.text = message;
  81. hud.label.numberOfLines = 0;
  82. hud.label.textColor = [UIColor whiteColor];
  83. hud.minSize = CGSizeMake(132.0f, 40.0f);
  84. hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  85. hud.bezelView.backgroundColor = HexRGBAlpha(0x000000, 0.8);
  86. [hud hideAnimated:YES afterDelay:PROMPT_TIME];
  87. dispatch_delay_block(DISPATCH_TIME_NOW, (int64_t)(PROMPT_TIME * NSEC_PER_SEC), ^{
  88. promptCompletion();
  89. });
  90. }
  91. @end