HUDHelper.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // HUDHelper.m
  3. //
  4. //
  5. // Created by Alexi on 12-11-28.
  6. // Copyright (c) 2012年 . All rights reserved.
  7. //
  8. #import "HUDHelper.h"
  9. #import "NSString+Common.h"
  10. #import "UIAlertView+BlocksKit.h"
  11. @implementation HUDHelper
  12. {
  13. void (^_tipCompletionBlock)(void);
  14. NSTimer *_tipCompletionBlockTimer;
  15. }
  16. static HUDHelper *_instance = nil;
  17. + (HUDHelper *)sharedInstance
  18. {
  19. @synchronized(_instance)
  20. {
  21. if (_instance == nil)
  22. {
  23. _instance = [[HUDHelper alloc] init];
  24. }
  25. return _instance;
  26. }
  27. }
  28. + (void)alert:(NSString *)msg
  29. {
  30. [HUDHelper alert:msg cancel:NSLocalizedString(@"Common.OK", nil)];
  31. }
  32. + (void)alert:(NSString *)msg action:(CommonVoidBlock)action
  33. {
  34. [HUDHelper alert:msg cancel:NSLocalizedString(@"Common.OK", nil) action:action];
  35. }
  36. + (void)alert:(NSString *)msg cancel:(NSString *)cancel
  37. {
  38. [HUDHelper alertTitle:NSLocalizedString(@"Common.Hint", nil) message:msg cancel:cancel];
  39. }
  40. + (void)alert:(NSString *)msg cancel:(NSString *)cancel action:(CommonVoidBlock)action
  41. {
  42. [HUDHelper alertTitle:NSLocalizedString(@"Common.Hint", nil) message:msg cancel:cancel action:action];
  43. }
  44. + (void)alertTitle:(NSString *)title message:(NSString *)msg cancel:(NSString *)cancel
  45. {
  46. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:cancel otherButtonTitles:nil, nil];
  47. [alert show];
  48. }
  49. + (void)alertTitle:(NSString *)title message:(NSString *)msg cancel:(NSString *)cancel action:(CommonVoidBlock)action
  50. {
  51. UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:title message:msg cancelButtonTitle:cancel otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  52. if (action)
  53. {
  54. action();
  55. }
  56. }];
  57. [alert show];
  58. }
  59. - (MBProgressHUD *)loading
  60. {
  61. return [self loading:nil];
  62. }
  63. - (MBProgressHUD *)loading:(NSString *)msg
  64. {
  65. return [self loading:msg inView:nil];
  66. }
  67. - (MBProgressHUD *)loading:(NSString *)msg inView:(UIView *)view
  68. {
  69. UIView *inView = view ? view : [BGBaseAppDelegate sharedAppDelegate].window;
  70. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
  71. dispatch_async(dispatch_get_main_queue(), ^{
  72. if (![NSString isEmpty:msg])
  73. {
  74. hud.mode = MBProgressHUDModeIndeterminate;
  75. hud.label.text = msg;
  76. }
  77. [inView addSubview:hud];
  78. [hud showAnimated:YES];
  79. // 超时自动消失
  80. // [hud hideAnimated:YES afterDelay:kRequestTimeOutTime];
  81. });
  82. return hud;
  83. }
  84. - (void)loading:(NSString *)msg delay:(CGFloat)seconds execute:(void (^)(void))exec completion:(void (^)(void))completion
  85. {
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. UIView *inView = [BGBaseAppDelegate sharedAppDelegate].window;
  88. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
  89. if (![NSString isEmpty:msg])
  90. {
  91. hud.mode = MBProgressHUDModeText;
  92. hud.label.text = msg;
  93. }
  94. [inView addSubview:hud];
  95. [hud showAnimated:YES];
  96. if (exec)
  97. {
  98. exec();
  99. }
  100. // 超时自动消失
  101. [hud hideAnimated:YES afterDelay:seconds];
  102. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  103. if (completion)
  104. {
  105. completion();
  106. }
  107. });
  108. });
  109. }
  110. - (void)stopLoading:(MBProgressHUD *)hud
  111. {
  112. [self stopLoading:hud message:nil];
  113. }
  114. - (void)stopLoading:(MBProgressHUD *)hud message:(NSString *)msg
  115. {
  116. [self stopLoading:hud message:msg delay:0 completion:nil];
  117. }
  118. - (void)stopLoading:(MBProgressHUD *)hud message:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)(void))completion
  119. {
  120. if (hud && hud.superview)
  121. {
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. if (![NSString isEmpty:msg])
  124. {
  125. hud.label.text = msg;
  126. hud.mode = MBProgressHUDModeText;
  127. }
  128. [hud hideAnimated:YES afterDelay:seconds];
  129. _syncHUD = nil;
  130. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  131. if (completion)
  132. {
  133. completion();
  134. }
  135. });
  136. });
  137. }
  138. else
  139. {
  140. if (completion)
  141. {
  142. completion();
  143. }
  144. }
  145. }
  146. - (MBProgressHUD *)tipMessage:(NSString *)msg
  147. {
  148. return [self tipMessage:msg delay:2];
  149. }
  150. - (MBProgressHUD *)tipMessage:(NSString *)msg delay:(CGFloat)seconds
  151. {
  152. return [self tipMessage:msg delay:seconds completion:nil];
  153. }
  154. - (MBProgressHUD *)tipMessage:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)(void))completion
  155. {
  156. NSAssert([NSThread isMainThread], @"Call HUD on main thread only");
  157. if ([NSString isEmpty:msg])
  158. {
  159. return nil;
  160. }
  161. MBProgressHUD *hud = [MBProgressHUD HUDForView:[BGBaseAppDelegate sharedAppDelegate].window];
  162. if (hud == nil) {
  163. hud = [[MBProgressHUD alloc] initWithView:[BGBaseAppDelegate sharedAppDelegate].window];
  164. hud.removeFromSuperViewOnHide = YES;
  165. }
  166. [[BGBaseAppDelegate sharedAppDelegate].window addSubview:hud];
  167. hud.mode = MBProgressHUDModeText;
  168. hud.label.text = msg;
  169. [hud showAnimated:YES];
  170. [hud hideAnimated:YES afterDelay:seconds];
  171. [_tipCompletionBlockTimer invalidate];
  172. if (_tipCompletionBlock) {
  173. _tipCompletionBlock();
  174. }
  175. _tipCompletionBlock = completion;
  176. if (completion) {
  177. _tipCompletionBlockTimer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(onTipTimer:) userInfo:nil repeats:NO];
  178. }
  179. return hud;
  180. }
  181. - (void)onTipTimer:(NSTimer *)timer {
  182. if (_tipCompletionBlock) {
  183. _tipCompletionBlock();
  184. _tipCompletionBlock = nil;
  185. }
  186. }
  187. #define kSyncHUDStartTag 100000
  188. // 网络请求
  189. - (void)syncLoading
  190. {
  191. [self syncLoading:nil];
  192. }
  193. - (void)syncLoading:(NSString *)msg
  194. {
  195. [self syncLoading:msg inView:nil];
  196. }
  197. - (void)syncLoading:(NSString *)msg inView:(UIView *)view
  198. {
  199. if (_syncHUD)
  200. {
  201. _syncHUD.tag++;
  202. if (![NSString isEmpty:msg])
  203. {
  204. _syncHUD.label.text = msg;
  205. _syncHUD.mode = MBProgressHUDModeText;
  206. }
  207. else
  208. {
  209. _syncHUD.label.text = nil;
  210. _syncHUD.mode = MBProgressHUDModeIndeterminate;
  211. }
  212. return;
  213. }
  214. _syncHUD = [self loading:msg inView:view];
  215. _syncHUD.tag = kSyncHUDStartTag;
  216. }
  217. - (void)syncStopLoading
  218. {
  219. [self syncStopLoadingMessage:nil delay:0 completion:nil];
  220. }
  221. - (void)syncStopLoadingMessage:(NSString *)msg
  222. {
  223. [self syncStopLoadingMessage:msg delay:1 completion:nil];
  224. }
  225. - (void)syncStopLoadingMessage:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)(void))completion
  226. {
  227. _syncHUD.tag--;
  228. if (_syncHUD.tag > kSyncHUDStartTag)
  229. {
  230. if (![NSString isEmpty:msg])
  231. {
  232. _syncHUD.label.text = msg;
  233. _syncHUD.mode = MBProgressHUDModeText;
  234. }
  235. else
  236. {
  237. _syncHUD.label.text = nil;
  238. _syncHUD.mode = MBProgressHUDModeIndeterminate;
  239. }
  240. }
  241. else
  242. {
  243. [self stopLoading:_syncHUD message:msg delay:seconds completion:completion];
  244. }
  245. }
  246. @end