UIViewController+HUB.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // UIViewController+HUB.m
  3. // ZhuDD
  4. //
  5. // Created by 张夏永 on 17/5/2.
  6. // Copyright © 2017年 ZXY. All rights reserved.
  7. //
  8. #import "UIViewController+HUB.h"
  9. #import "MBProgressHUD.h"
  10. #import <objc/runtime.h>
  11. @implementation UIViewController (HUB)
  12. static const void *HttpRequestHUDKey = &HttpRequestHUDKey;
  13. - (MBProgressHUD *)HUD{
  14. return objc_getAssociatedObject(self, HttpRequestHUDKey);
  15. }
  16. - (void)ShowHudInView:(UIView *)view hint:(NSString *)hint{
  17. [self HideHud];
  18. MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
  19. // HUD.backgroundView.userInteractionEnabled=NO;
  20. // HUD.userInteractionEnabled = NO;
  21. HUD.removeFromSuperViewOnHide=YES;
  22. HUD.bezelView.color=[UIColor blackColor];
  23. HUD.activityIndicatorColor=[UIColor whiteColor];
  24. HUD.label.text = hint;
  25. [view addSubview:HUD];
  26. [HUD showAnimated:YES];
  27. [self setHUD:HUD];
  28. }
  29. - (void)setHUD:(MBProgressHUD *)HUD{
  30. objc_setAssociatedObject(self, HttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  31. }
  32. - (void)ShowReminder:(NSString *)reminder waitTime:(float)t with:(void (^)(void))block{
  33. [self HideHud];
  34. if (reminder.length==0) {
  35. return;
  36. }
  37. //显示提示信息
  38. UIView *view = [[UIApplication sharedApplication].delegate window];
  39. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  40. // Configure for text only and offset down
  41. hud.mode = MBProgressHUDModeText;
  42. hud.bezelView.color=[UIColor blackColor];
  43. hud.label.font=[UIFont systemFontOfSize:14];
  44. hud.label.textColor=[UIColor whiteColor];
  45. hud.label.text= reminder;
  46. hud.label.numberOfLines=0;
  47. hud.margin = 10.f;
  48. hud.removeFromSuperViewOnHide = YES;
  49. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(t * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  50. [hud hideAnimated:YES];
  51. if(block){
  52. block();
  53. }
  54. });
  55. }
  56. -(void)ShowBadNetWork{
  57. [self HideHud];
  58. UIView *view = [[UIApplication sharedApplication].delegate window];
  59. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  60. hud.defaultMotionEffectsEnabled=YES;
  61. // Configure for text only and offset down
  62. hud.mode = MBProgressHUDModeText;
  63. hud.bezelView.color=[UIColor blackColor];
  64. hud.label.font=[UIFont systemFontOfSize:14];
  65. hud.label.textColor=[UIColor whiteColor];
  66. hud.label.text=@"网络不佳,请稍后重试";
  67. hud.margin = 10.f;
  68. hud.removeFromSuperViewOnHide = YES;
  69. [hud hideAnimated:YES afterDelay:1.5];
  70. }
  71. - (void)ShowHint:(NSString *)hint
  72. {
  73. [self HideHud];
  74. //显示提示信息
  75. UIView *view = [[UIApplication sharedApplication].delegate window];
  76. MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
  77. HUD.defaultMotionEffectsEnabled=YES;
  78. // HUD.backgroundView.userInteractionEnabled=NO;
  79. HUD.userInteractionEnabled=NO;
  80. HUD.bezelView.color=[UIColor blackColor];
  81. [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
  82. HUD.label.text = hint;
  83. HUD.label.textColor=[UIColor whiteColor];
  84. [view addSubview:HUD];
  85. [HUD showAnimated:YES];
  86. [HUD hideAnimated:YES afterDelay:15.0];
  87. [self setHUD:HUD];
  88. }
  89. - (void)ShowHint:(NSString *)hint vc:(UIViewController *)vc
  90. {
  91. [self HideHud];
  92. //显示提示信息
  93. UIView *view = vc.view;
  94. MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
  95. HUD.defaultMotionEffectsEnabled=YES;
  96. // HUD.userInteractionEnabled=YES;
  97. HUD.bezelView.color=[UIColor blackColor];
  98. [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
  99. HUD.label.text = hint;
  100. HUD.label.textColor=[UIColor whiteColor];
  101. [view addSubview:HUD];
  102. [HUD showAnimated:YES];
  103. [HUD hideAnimated:YES afterDelay:15.0];
  104. [self setHUD:HUD];
  105. }
  106. - (void)ShowHint:(NSString *)hint yOffset:(float)yOffset {
  107. //显示提示信息
  108. [self HideHud];
  109. UIView *view = [[UIApplication sharedApplication].delegate window];
  110. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  111. hud.defaultMotionEffectsEnabled=YES;
  112. // Configure for text only and offset down
  113. hud.mode = MBProgressHUDModeText;
  114. hud.label.text = hint;
  115. hud.label.font=[UIFont systemFontOfSize:14];
  116. hud.bezelView.color=[UIColor blackColor];
  117. hud.label.textColor=[UIColor whiteColor];
  118. hud.margin = 10.f;
  119. hud.offset=CGPointMake(hud.offset.x, hud.offset.y+yOffset);
  120. hud.removeFromSuperViewOnHide = YES;
  121. [hud hideAnimated:YES afterDelay:2.0];
  122. }
  123. - (void)HideHud {
  124. [[self HUD] removeFromSuperview];
  125. }
  126. - (void)hideWithMsg:(NSString *)msg After:(CGFloat)delay {
  127. //显示提示信息
  128. UIView *view = [[UIApplication sharedApplication].delegate window];
  129. [MBProgressHUD hideHUDForView:view animated:NO];
  130. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  131. hud.defaultMotionEffectsEnabled=YES;
  132. // Configure for text only and offset down
  133. hud.mode = MBProgressHUDModeText;
  134. hud.label.text = msg;
  135. hud.label.font=[UIFont systemFontOfSize:14];
  136. hud.bezelView.color=[UIColor blackColor];
  137. hud.label.textColor=[UIColor whiteColor];
  138. hud.margin = 10.f;
  139. hud.removeFromSuperViewOnHide = YES;
  140. [hud hideAnimated:YES afterDelay:delay];
  141. }
  142. @end