MBProgressHUD+RQExtension.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // MBProgressHUD+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/23.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "MBProgressHUD+RQExtension.h"
  9. #import "NSError+RQExtension.h"
  10. @implementation MBProgressHUD (RQExtension)
  11. #pragma mark - Added To window
  12. /// 提示信息
  13. + (MBProgressHUD *)rq_showTips:(NSString *)tipStr{
  14. return [self rq_showTips:tipStr addedToView:nil];
  15. }
  16. /// 提示错误
  17. + (MBProgressHUD *)rq_showErrorTips:(NSError *)error{
  18. return [self rq_showErrorTips:error addedToView:nil];
  19. }
  20. /// 进度view
  21. + (MBProgressHUD *)rq_showProgressHUD:(NSString *)titleStr
  22. {
  23. return [self rq_showProgressHUD:titleStr addedToView:nil];
  24. }
  25. /// hide hud
  26. + (void)rq_hideHUD
  27. {
  28. [self rq_hideHUDForView:nil];
  29. }
  30. #pragma mark - Added To Special View
  31. /// 提示信息
  32. + (MBProgressHUD *)rq_showTips:(NSString *)tipStr addedToView:(UIView *)view
  33. {
  34. return [self _showHUDWithTips:tipStr isAutomaticHide:YES addedToView:view];
  35. }
  36. /// 提示错误
  37. + (MBProgressHUD *)rq_showErrorTips:(NSError *)error addedToView:(UIView *)view
  38. {
  39. return [self _showHUDWithTips:[self rq_tipsFromError:error] isAutomaticHide:YES addedToView:view];
  40. }
  41. /// 进度view
  42. + (MBProgressHUD *)rq_showProgressHUD:(NSString *)titleStr addedToView:(UIView *)view{
  43. return [self _showHUDWithTips:titleStr isAutomaticHide:NO addedToView:view];
  44. }
  45. // 隐藏HUD
  46. + (void)rq_hideHUDForView:(UIView *)view
  47. {
  48. [self hideHUDForView:[self _willShowingToViewWithSourceView:view] animated:YES];
  49. }
  50. #pragma mark - 辅助方法
  51. /// 获取将要显示的view
  52. + (UIView *)_willShowingToViewWithSourceView:(UIView *)sourceView
  53. {
  54. if (sourceView) return sourceView;
  55. sourceView = [[UIApplication sharedApplication].delegate window];
  56. if (!sourceView) sourceView = [[[UIApplication sharedApplication] windows] lastObject];
  57. return sourceView;
  58. }
  59. + (instancetype )_showHUDWithTips:(NSString *)tipStr isAutomaticHide:(BOOL) isAutomaticHide addedToView:(UIView *)view
  60. {
  61. view = [self _willShowingToViewWithSourceView:view];
  62. /// 也可以show之前 hid掉之前的
  63. [self rq_hideHUDForView:view];
  64. MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:view animated:YES];
  65. HUD.mode = isAutomaticHide?MBProgressHUDModeText:MBProgressHUDModeIndeterminate;
  66. HUD.animationType = MBProgressHUDAnimationZoom;
  67. HUD.label.font = isAutomaticHide?RQMediumFont(17.0f):RQMediumFont(14.0f);
  68. HUD.label.textColor = [UIColor whiteColor];
  69. HUD.contentColor = [UIColor whiteColor];
  70. HUD.label.text = tipStr;
  71. HUD.bezelView.layer.cornerRadius = 8.0f;
  72. HUD.bezelView.layer.masksToBounds = YES;
  73. HUD.bezelView.color = RQColorAlpha(0, 0, 0, .6f);
  74. HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  75. HUD.minSize =isAutomaticHide?CGSizeMake([UIScreen mainScreen].bounds.size.width-96.0f, 60):CGSizeMake(120, 120);
  76. HUD.margin = 18.2f;
  77. HUD.removeFromSuperViewOnHide = YES;
  78. if (isAutomaticHide) [HUD hideAnimated:YES afterDelay:1.0f];
  79. return HUD;
  80. }
  81. #pragma mark - 辅助属性
  82. + (NSString *)rq_tipsFromError:(NSError *)error{
  83. return [NSError rq_tipsFromError:error];
  84. }
  85. + (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
  86. {
  87. if (view == nil) view = [UIApplication sharedApplication].keyWindow;
  88. // 快速显示一个提示信息
  89. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  90. hud.label.text = text;
  91. // 设置图片
  92. hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
  93. // 再设置模式
  94. hud.mode = MBProgressHUDModeCustomView;
  95. // 隐藏时候从父控件中移除
  96. hud.removeFromSuperViewOnHide = YES;
  97. // 1秒之后再消失
  98. [hud hideAnimated:YES afterDelay:0.7];
  99. }
  100. #pragma mark 显示错误信息
  101. + (void)showError:(NSString *)error toView:(UIView *)view{
  102. [self show:error icon:@"error.png" view:view];
  103. }
  104. + (void)showSuccess:(NSString *)success toView:(UIView *)view
  105. {
  106. [self show:success icon:@"success.png" view:view];
  107. }
  108. #pragma mark 显示一些信息
  109. + (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
  110. if (view == nil) view = [UIApplication sharedApplication].keyWindow;
  111. // 快速显示一个提示信息
  112. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  113. hud.label.text = message;
  114. // 隐藏时候从父控件中移除
  115. hud.removeFromSuperViewOnHide = YES;
  116. // YES代表需要蒙版效果
  117. // hud.dimBackground = YES;
  118. return hud;
  119. }
  120. #pragma mark 显示错误信息
  121. + (void)showError:(NSString *)error ToView:(UIView *)view {
  122. [self showCustomIcon:@"mbp_error.png" Title:error ToView:view];
  123. }
  124. + (void)showSuccess:(NSString *)success ToView:(UIView *)view {
  125. [self showCustomIcon:@"mbp_success.png" Title:success ToView:view];
  126. }
  127. #pragma mark 显示一些信息 不自动消失
  128. + (MBProgressHUD *)showMessage:(NSString *)message ToView:(UIView *)view {
  129. if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
  130. // 快速显示一个提示信息
  131. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  132. hud.label.text = message;
  133. // 隐藏时候从父控件中移除
  134. hud.removeFromSuperViewOnHide = YES;
  135. // YES代表需要蒙版效果
  136. // hud.dimBackground = YES;
  137. if ([[NSThread currentThread] isEqual:[NSThread mainThread]]) {
  138. hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
  139. hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:.2f];
  140. }
  141. return hud;
  142. }
  143. //加载视图
  144. +(void)showLoadToView:(UIView *)view{
  145. [self showMessage:@"加载中..." ToView:view];
  146. }
  147. //快速显示一条提示信息
  148. + (void)showAutoMessage:(NSString *)message{
  149. [self showAutoMessage:message ToView:nil];
  150. }
  151. //自动消失提示,无图
  152. + (void)showAutoMessage:(NSString *)message ToView:(UIView *)view{
  153. [self showMessage:message ToView:view RemainTime:2 Model:MBProgressHUDModeText];
  154. }
  155. //自定义停留时间,有图
  156. +(void)showIconMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time{
  157. [self showMessage:message ToView:view RemainTime:time Model:MBProgressHUDModeIndeterminate];
  158. }
  159. //自定义停留时间,无图
  160. +(void)showMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time{
  161. [self showMessage:message ToView:view RemainTime:time Model:MBProgressHUDModeText];
  162. }
  163. +(void)showMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time Model:(MBProgressHUDMode)model{
  164. if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
  165. // 快速显示一个提示信息
  166. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  167. hud.detailsLabel.text = message? : @"";
  168. //模式
  169. hud.mode = model;
  170. // 隐藏时候从父控件中移除
  171. hud.removeFromSuperViewOnHide = YES;
  172. hud.userInteractionEnabled = NO;
  173. // YES代表需要蒙版效果
  174. // hud.dimBackground = YES;
  175. // hud.backgroundColor = [];
  176. // if ([[NSThread currentThread] isEqual:[NSThread mainThread]]) {
  177. // hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
  178. // hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:.2f];
  179. // }
  180. // X秒之后再消失
  181. [hud hideAnimated:YES afterDelay:time];
  182. }
  183. + (void)showCustomIcon:(NSString *)iconName Title:(NSString *)title ToView:(UIView *)view
  184. {
  185. if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
  186. // 快速显示一个提示信息
  187. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  188. hud.label.text = title;
  189. // 设置图片
  190. hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
  191. // 再设置模式
  192. hud.mode = MBProgressHUDModeCustomView;
  193. // 隐藏时候从父控件中移除
  194. hud.removeFromSuperViewOnHide = YES;
  195. // 2秒之后再消失
  196. [hud hideAnimated:YES afterDelay:2.1];
  197. hud.userInteractionEnabled = NO;
  198. }
  199. + (void)hideHUDForView:(UIView *)view
  200. {
  201. if (view == nil) view = (UIView*)[UIApplication sharedApplication].delegate.window;
  202. [self hideHUDForView:view animated:YES];
  203. }
  204. + (void)hideHUD
  205. {
  206. [self hideHUDForView:nil];
  207. }
  208. @end