XCHudHelper.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // XFHudHelper.m
  3. //
  4. // Created by TopDev on 10/23/14.
  5. // Copyright (c) 2014 TopDev. All rights reserved.
  6. //
  7. #import "XCHudHelper.h"
  8. @implementation XCHudHelper
  9. + (XCHudHelper *)sharedInstance {
  10. static XCHudHelper *_instance = nil;
  11. @synchronized (self) {
  12. if (_instance == nil) {
  13. _instance = [[self alloc] init];
  14. }
  15. }
  16. return _instance;
  17. }
  18. - (void)showHudAcitivityOnWindow
  19. {
  20. [self showHudOnWindow:nil image:nil acitivity:YES autoHideTime:0];
  21. }
  22. - (void)showHudOnWindow:(NSString *)caption
  23. image:(UIImage *)image
  24. acitivity:(BOOL)active
  25. autoHideTime:(NSTimeInterval)time1 {
  26. if (_hud) {
  27. [_hud hideAnimated:NO];
  28. }
  29. self.hud = [[MBProgressHUD alloc] initWithView:[[UIApplication sharedApplication] delegate].window];
  30. [[[UIApplication sharedApplication] delegate].window addSubview:self.hud];
  31. self.hud.label.text = caption;
  32. self.hud.customView = [[UIImageView alloc] initWithImage:image];
  33. self.hud.customView.bounds = CGRectMake(0, 0, 100, 100);
  34. self.hud.mode = image ? MBProgressHUDModeCustomView : MBProgressHUDModeIndeterminate;
  35. self.hud.animationType = MBProgressHUDAnimationFade;
  36. self.hud.removeFromSuperViewOnHide = YES;
  37. [self.hud showAnimated:YES];
  38. if (time1 > 0) {
  39. [self.hud hideAnimated:YES afterDelay:time1];
  40. }
  41. }
  42. - (void)showHudOnView:(UIView *)view
  43. caption:(NSString *)caption
  44. image:(UIImage *)image
  45. acitivity:(BOOL)active
  46. autoHideTime:(NSTimeInterval)time1 {
  47. if (_hud) {
  48. [_hud hideAnimated:NO];
  49. }
  50. self.hud = [[MBProgressHUD alloc] initWithView:view];
  51. [view addSubview:self.hud];
  52. self.hud.label.text = caption;
  53. self.hud.customView = [[UIImageView alloc] initWithImage:image];
  54. self.hud.customView.bounds = CGRectMake(0, 0, 100, 100);
  55. self.hud.mode = image ? MBProgressHUDModeCustomView : MBProgressHUDModeIndeterminate;
  56. self.hud.animationType = MBProgressHUDAnimationFade;
  57. [self.hud showAnimated:YES];
  58. if (time1 > 0) {
  59. [self.hud hideAnimated:YES afterDelay:time1];
  60. }
  61. }
  62. - (void)setCaption:(NSString *)caption {
  63. self.hud.label.text = caption;
  64. }
  65. - (void)hideHud {
  66. if (_hud) {
  67. [_hud hideAnimated:YES];
  68. }
  69. }
  70. - (void)hideHudAfter:(NSTimeInterval)time1 {
  71. if (_hud) {
  72. [_hud hideAnimated:YES afterDelay:time1];
  73. }
  74. }
  75. + (void)showSuccess:(NSString *)success
  76. {
  77. [self showSuccess:success toView:nil];
  78. }
  79. + (void)showError:(NSString *)error
  80. {
  81. [self showError:error toView:nil];
  82. }
  83. + (void)showError:(NSString *)error toView:(UIView *)view{
  84. [self show:error icon:@"error.png" view:view];
  85. }
  86. + (void)showSuccess:(NSString *)success toView:(UIView *)view
  87. {
  88. [self show:success icon:@"success.png" view:view];
  89. }
  90. #pragma mark 显示信息
  91. + (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
  92. {
  93. if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
  94. // 快速显示一个提示信息
  95. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
  96. hud.label.text = text;
  97. // 设置图片
  98. hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
  99. // 再设置模式
  100. hud.mode = MBProgressHUDModeCustomView;
  101. // 隐藏时候从父控件中移除
  102. hud.removeFromSuperViewOnHide = YES;
  103. // 1.0秒之后再消失
  104. [hud hideAnimated:YES afterDelay:1.0];
  105. }
  106. + (void)showMessage:(NSString *)message
  107. {
  108. [self show:message icon:@"" view:nil];
  109. }
  110. + (void)showMessage:(NSString *)message toView:(UIView *)view {
  111. if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
  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. // 100秒之后再消失
  120. [hud hideAnimated:YES afterDelay:100.0];
  121. }
  122. + (void)hideHUD
  123. {
  124. UIView *view = [[UIApplication sharedApplication].windows lastObject];
  125. [MBProgressHUD hideHUDForView:view animated:YES];
  126. }
  127. @end