BGHUDHelper.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // BGHUDHelper.m
  3. //
  4. //
  5. // Created by Alexi on 12-11-28.
  6. // Copyright (c) 2012年 . All rights reserved.
  7. //
  8. #import "BGHUDHelper.h"
  9. #import "NSString+Common.h"
  10. #import "UIAlertView+BlocksKit.h"
  11. @implementation BGHUDHelper
  12. static BGHUDHelper *_instance = nil;
  13. + (BGHUDHelper *)sharedInstance
  14. {
  15. @synchronized(_instance)
  16. {
  17. if (_instance == nil)
  18. {
  19. _instance = [[BGHUDHelper alloc] init];
  20. }
  21. return _instance;
  22. }
  23. }
  24. + (void)alert:(NSString *)msg
  25. {
  26. [BGHUDHelper alert:msg cancel:ASLocalizedString(@"确定")];
  27. }
  28. + (void)alert:(NSString *)msg action:(FWVoidBlock)action
  29. {
  30. [BGHUDHelper alert:msg cancel:ASLocalizedString(@"确定")action:action];
  31. }
  32. + (void)alert:(NSString *)msg cancel:(NSString *)cancel
  33. {
  34. [BGHUDHelper alertTitle:ASLocalizedString(@"提示")message:msg cancel:cancel];
  35. }
  36. + (void)alert:(NSString *)msg cancel:(NSString *)cancel action:(FWVoidBlock)action
  37. {
  38. [BGHUDHelper alertTitle:ASLocalizedString(@"提示")message:msg cancel:cancel action:action];
  39. }
  40. + (void)alertTitle:(NSString *)title message:(NSString *)msg cancel:(NSString *)cancel
  41. {
  42. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:cancel otherButtonTitles:nil, nil];
  43. [alert show];
  44. }
  45. + (void)alertTitle:(NSString *)title message:(NSString *)msg cancel:(NSString *)cancel action:(FWVoidBlock)action
  46. {
  47. UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:title message:msg cancelButtonTitle:cancel otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
  48. if (action)
  49. {
  50. action();
  51. }
  52. }];
  53. [alert show];
  54. }
  55. - (MBProgressHUD *)loading
  56. {
  57. return [self loading:nil];
  58. }
  59. - (MBProgressHUD *)loading:(NSString *)msg
  60. {
  61. return [self loading:msg inView:nil];
  62. }
  63. - (MBProgressHUD *)loading:(NSString *)msg inView:(UIView *)view
  64. {
  65. // 判断是否有悬浮窗
  66. UIView *tmpView = [AppDelegate sharedAppDelegate].sus_window.rootViewController ? [AppDelegate sharedAppDelegate].sus_window : [AppDelegate sharedAppDelegate].window;
  67. // 判断是否有小屏悬浮窗
  68. UIView *tmpView2 = [AppDelegate sharedAppDelegate].sus_window.isSmallSusWindow ? [AppDelegate sharedAppDelegate].window : tmpView;
  69. // 判断是否有传入view
  70. UIView *inView = view ? view : tmpView2;
  71. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
  72. dispatch_async(dispatch_get_main_queue(), ^{
  73. if (![NSString isEmpty:msg])
  74. {
  75. hud.mode = MBProgressHUDModeIndeterminate;
  76. hud.label.text = msg;
  77. }
  78. [inView addSubview:hud];
  79. [hud showAnimated:YES];
  80. // 超时自动消失
  81. // [hud hide:YES afterDelay:kRequestTimeOutTime];
  82. });
  83. return hud;
  84. }
  85. - (void)loading:(NSString *)msg delay:(CGFloat)seconds execute:(void (^)())exec completion:(void (^)())completion
  86. {
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. // 判断是否有悬浮窗
  89. UIView *tmpView = [AppDelegate sharedAppDelegate].sus_window.rootViewController ? [AppDelegate sharedAppDelegate].sus_window : [AppDelegate sharedAppDelegate].window;
  90. // 判断是否有小屏悬浮窗
  91. UIView *inView = [AppDelegate sharedAppDelegate].sus_window.isSmallSusWindow ? [AppDelegate sharedAppDelegate].window : tmpView;
  92. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
  93. if (![NSString isEmpty:msg])
  94. {
  95. hud.mode = MBProgressHUDModeText;
  96. hud.label.text = msg;
  97. }
  98. [inView addSubview:hud];
  99. [hud showAnimated:YES];
  100. if (exec)
  101. {
  102. exec();
  103. }
  104. // 超时自动消失
  105. [hud hideAnimated:YES afterDelay:seconds];
  106. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  107. if (completion)
  108. {
  109. completion();
  110. }
  111. });
  112. });
  113. }
  114. - (void)stopLoading:(MBProgressHUD *)hud
  115. {
  116. [self stopLoading:hud message:nil];
  117. }
  118. - (void)stopLoading:(MBProgressHUD *)hud message:(NSString *)msg
  119. {
  120. [self stopLoading:hud message:msg delay:0 completion:nil];
  121. }
  122. - (void)stopLoading:(MBProgressHUD *)hud message:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)())completion
  123. {
  124. if (hud && hud.superview)
  125. {
  126. dispatch_async(dispatch_get_main_queue(), ^{
  127. if (![NSString isEmpty:msg])
  128. {
  129. hud.label.text = msg;
  130. hud.mode = MBProgressHUDModeText;
  131. }
  132. [hud hideAnimated:YES afterDelay:seconds];
  133. _syncHUD = nil;
  134. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  135. if (completion)
  136. {
  137. completion();
  138. }
  139. });
  140. });
  141. }
  142. }
  143. - (void)tipMessage:(NSString *)msg
  144. {
  145. [self tipMessage:msg delay:2];
  146. }
  147. - (void)tipMessage:(NSString *)msg delay:(CGFloat)seconds
  148. {
  149. [self tipMessage:msg delay:seconds completion:nil];
  150. }
  151. - (void)tipMessage:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)())completion
  152. {
  153. if ([BGUtils isBlankString:msg])
  154. {
  155. return;
  156. }
  157. if ([msg isEqualToString:@"(null)"])
  158. {
  159. return;
  160. }
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. // 判断是否有悬浮窗
  163. UIView *tmpView = [AppDelegate sharedAppDelegate].sus_window.rootViewController ? [AppDelegate sharedAppDelegate].sus_window : [AppDelegate sharedAppDelegate].window;
  164. // 判断是否有小屏悬浮窗
  165. UIView *inView = [AppDelegate sharedAppDelegate].sus_window.isSmallSusWindow ? [AppDelegate sharedAppDelegate].window : tmpView;
  166. MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
  167. [inView addSubview:hud];
  168. hud.mode = MBProgressHUDModeText;
  169. hud.label.text = msg;
  170. hud.label.numberOfLines = 0;
  171. [hud showAnimated:YES];
  172. [hud hideAnimated:YES afterDelay:seconds];
  173. CommonRelease(HUD);
  174. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  175. if (completion)
  176. {
  177. completion();
  178. }
  179. });
  180. });
  181. }
  182. #define kSyncHUDStartTag 100000
  183. // 网络请求
  184. - (void)syncLoading
  185. {
  186. [self syncLoading:nil];
  187. }
  188. - (void)syncLoading:(NSString *)msg
  189. {
  190. [self syncLoading:msg inView:nil];
  191. }
  192. - (void)syncLoading:(NSString *)msg inView:(UIView *)view
  193. {
  194. if (_syncHUD)
  195. {
  196. dispatch_async(dispatch_get_main_queue(), ^{
  197. //主线程执行
  198. _syncHUD.tag++;
  199. if (![NSString isEmpty:msg])
  200. {
  201. _syncHUD.label.text = msg;
  202. _syncHUD.mode = MBProgressHUDModeText;
  203. }
  204. else
  205. {
  206. _syncHUD.label.text = nil;
  207. _syncHUD.mode = MBProgressHUDModeIndeterminate;
  208. }
  209. });
  210. return;
  211. }
  212. _syncHUD = [self loading:msg inView:view];
  213. _syncHUD.tag = kSyncHUDStartTag;
  214. }
  215. - (void)syncStopLoading
  216. {
  217. [self syncStopLoadingMessage:nil delay:0 completion:nil];
  218. }
  219. - (void)syncStopLoadingMessage:(NSString *)msg
  220. {
  221. [self syncStopLoadingMessage:msg delay:1 completion:nil];
  222. }
  223. - (void)syncStopLoadingMessage:(NSString *)msg delay:(CGFloat)seconds completion:(void (^)())completion
  224. {
  225. dispatch_async(dispatch_get_main_queue(), ^{
  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. }
  247. @end