MBProgressHUD+RQExtension.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.numberOfLines = 0;
  69. HUD.label.textColor = [UIColor whiteColor];
  70. HUD.contentColor = [UIColor whiteColor];
  71. HUD.label.text = tipStr;
  72. HUD.bezelView.layer.cornerRadius = 8.0f;
  73. HUD.bezelView.layer.masksToBounds = YES;
  74. HUD.bezelView.color = RQColorAlpha(0, 0, 0, .6f);
  75. HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  76. HUD.minSize =isAutomaticHide?CGSizeMake([UIScreen mainScreen].bounds.size.width-96.0f, 60):CGSizeMake(120, 120);
  77. HUD.margin = 18.2f;
  78. HUD.removeFromSuperViewOnHide = YES;
  79. if (isAutomaticHide) [HUD hideAnimated:YES afterDelay:2.0f];
  80. return HUD;
  81. }
  82. #pragma mark - 辅助属性
  83. + (NSString *)rq_tipsFromError:(NSError *)error{
  84. return [NSError rq_tipsFromError:error];
  85. }
  86. + (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
  87. {
  88. if (view == nil) view = [UIApplication sharedApplication].keyWindow;
  89. // 快速显示一个提示信息
  90. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  91. hud.label.text = text;
  92. // 设置图片
  93. hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
  94. // 再设置模式
  95. hud.mode = MBProgressHUDModeCustomView;
  96. // 隐藏时候从父控件中移除
  97. hud.removeFromSuperViewOnHide = YES;
  98. // 1秒之后再消失
  99. [hud hideAnimated:YES afterDelay:0.7];
  100. }
  101. #pragma mark 显示错误信息
  102. + (void)showError:(NSString *)error toView:(UIView *)view{
  103. [self show:error icon:@"error.png" view:view];
  104. }
  105. + (void)showSuccess:(NSString *)success toView:(UIView *)view
  106. {
  107. [self show:success icon:@"success.png" view:view];
  108. }
  109. #pragma mark 显示一些信息
  110. + (MBProgressHUD *)showMessag:(NSString *)message toView:(UIView *)view {
  111. if (view == nil) view = [UIApplication sharedApplication].keyWindow;
  112. // 快速显示一个提示信息
  113. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  114. hud.label.text = message;
  115. // 隐藏时候从父控件中移除
  116. hud.removeFromSuperViewOnHide = YES;
  117. // YES代表需要蒙版效果
  118. // hud.dimBackground = YES;
  119. return hud;
  120. }
  121. @end