MBHudDemoViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //
  2. // MBHudDemoViewController.m
  3. // HudDemo
  4. //
  5. // Created by Matej Bukovinski on 30.9.09.
  6. // Copyright © 2009-2016 Matej Bukovinski. All rights reserved.
  7. //
  8. #import "MBHudDemoViewController.h"
  9. #import "MBProgressHUD.h"
  10. #import <QuartzCore/QuartzCore.h>
  11. @interface MBExample : NSObject
  12. @property (nonatomic, copy) NSString *title;
  13. @property (nonatomic, assign) SEL selector;
  14. @end
  15. @implementation MBExample
  16. + (instancetype)exampleWithTitle:(NSString *)title selector:(SEL)selector {
  17. MBExample *example = [[self class] new];
  18. example.title = title;
  19. example.selector = selector;
  20. return example;
  21. }
  22. @end
  23. @interface MBHudDemoViewController () <NSURLSessionDelegate>
  24. @property (nonatomic, strong) NSArray<NSArray<MBExample *> *> *examples;
  25. // Atomic, because it may be canceled from main thread, flag is read on a background thread
  26. @property (atomic, assign) BOOL canceled;
  27. @end
  28. @implementation MBHudDemoViewController
  29. #pragma mark - Lifecycle
  30. - (void)awakeFromNib {
  31. [super awakeFromNib];
  32. self.examples =
  33. @[@[[MBExample exampleWithTitle:@"Indeterminate mode" selector:@selector(indeterminateExample)],
  34. [MBExample exampleWithTitle:@"With label" selector:@selector(labelExample)],
  35. [MBExample exampleWithTitle:@"With details label" selector:@selector(detailsLabelExample)]],
  36. @[[MBExample exampleWithTitle:@"Determinate mode" selector:@selector(determinateExample)],
  37. [MBExample exampleWithTitle:@"Annular determinate mode" selector:@selector(annularDeterminateExample)],
  38. [MBExample exampleWithTitle:@"Bar determinate mode" selector:@selector(barDeterminateExample)]],
  39. @[[MBExample exampleWithTitle:@"Text only" selector:@selector(textExample)],
  40. [MBExample exampleWithTitle:@"Custom view" selector:@selector(customViewExample)],
  41. [MBExample exampleWithTitle:@"With action button" selector:@selector(cancelationExample)],
  42. [MBExample exampleWithTitle:@"Mode switching" selector:@selector(modeSwitchingExample)]],
  43. @[[MBExample exampleWithTitle:@"On window" selector:@selector(windowExample)],
  44. [MBExample exampleWithTitle:@"NSURLSession" selector:@selector(networkingExample)],
  45. [MBExample exampleWithTitle:@"Determinate with NSProgress" selector:@selector(determinateNSProgressExample)],
  46. [MBExample exampleWithTitle:@"Dim background" selector:@selector(dimBackgroundExample)],
  47. [MBExample exampleWithTitle:@"Colored" selector:@selector(colorExample)]]
  48. ];
  49. }
  50. #pragma mark - Examples
  51. - (void)indeterminateExample {
  52. // Show the HUD on the root view (self.view is a scrollable table view and thus not suitable,
  53. // as the HUD would move with the content as we scroll).
  54. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  55. // Fire off an asynchronous task, giving UIKit the opportunity to redraw wit the HUD added to the
  56. // view hierarchy.
  57. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  58. // Do something useful in the background
  59. [self doSomeWork];
  60. // IMPORTANT - Dispatch back to the main thread. Always access UI
  61. // classes (including MBProgressHUD) on the main thread.
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. [hud hideAnimated:YES];
  64. });
  65. });
  66. }
  67. - (void)labelExample {
  68. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  69. // Set the label text.
  70. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  71. // You can also adjust other label properties if needed.
  72. // hud.label.font = [UIFont italicSystemFontOfSize:16.f];
  73. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  74. [self doSomeWork];
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. [hud hideAnimated:YES];
  77. });
  78. });
  79. }
  80. - (void)detailsLabelExample {
  81. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  82. // Set the label text.
  83. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  84. // Set the details label text. Let's make it multiline this time.
  85. hud.detailsLabel.text = NSLocalizedString(@"Parsing data\n(1/1)", @"HUD title");
  86. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  87. [self doSomeWork];
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. [hud hideAnimated:YES];
  90. });
  91. });
  92. }
  93. - (void)windowExample {
  94. // Covers the entire screen. Similar to using the root view controller view.
  95. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];
  96. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  97. [self doSomeWork];
  98. dispatch_async(dispatch_get_main_queue(), ^{
  99. [hud hideAnimated:YES];
  100. });
  101. });
  102. }
  103. - (void)determinateExample {
  104. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  105. // Set the determinate mode to show task progress.
  106. hud.mode = MBProgressHUDModeDeterminate;
  107. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  108. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  109. // Do something useful in the background and update the HUD periodically.
  110. [self doSomeWorkWithProgress];
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. [hud hideAnimated:YES];
  113. });
  114. });
  115. }
  116. - (void)determinateNSProgressExample {
  117. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  118. // Set the determinate mode to show task progress.
  119. hud.mode = MBProgressHUDModeDeterminate;
  120. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  121. // Set up NSProgress
  122. NSProgress *progressObject = [NSProgress progressWithTotalUnitCount:100];
  123. hud.progressObject = progressObject;
  124. // Configure a cancel button.
  125. [hud.button setTitle:NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
  126. [hud.button addTarget:progressObject action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
  127. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  128. // Do something useful in the background and update the HUD periodically.
  129. [self doSomeWorkWithProgressObject:progressObject];
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. [hud hideAnimated:YES];
  132. });
  133. });
  134. }
  135. - (void)annularDeterminateExample {
  136. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  137. // Set the annular determinate mode to show task progress.
  138. hud.mode = MBProgressHUDModeAnnularDeterminate;
  139. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  140. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  141. // Do something useful in the background and update the HUD periodically.
  142. [self doSomeWorkWithProgress];
  143. dispatch_async(dispatch_get_main_queue(), ^{
  144. [hud hideAnimated:YES];
  145. });
  146. });
  147. }
  148. - (void)barDeterminateExample {
  149. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  150. // Set the bar determinate mode to show task progress.
  151. hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
  152. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  153. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  154. // Do something useful in the background and update the HUD periodically.
  155. [self doSomeWorkWithProgress];
  156. dispatch_async(dispatch_get_main_queue(), ^{
  157. [hud hideAnimated:YES];
  158. });
  159. });
  160. }
  161. - (void)customViewExample {
  162. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  163. // Set the custom view mode to show any view.
  164. hud.mode = MBProgressHUDModeCustomView;
  165. // Set an image view with a checkmark.
  166. UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  167. hud.customView = [[UIImageView alloc] initWithImage:image];
  168. // Looks a bit nicer if we make it square.
  169. hud.square = YES;
  170. // Optional label text.
  171. hud.label.text = NSLocalizedString(@"Done", @"HUD done title");
  172. [hud hideAnimated:YES afterDelay:3.f];
  173. }
  174. - (void)textExample {
  175. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  176. // Set the text mode to show only text.
  177. hud.mode = MBProgressHUDModeText;
  178. hud.label.text = NSLocalizedString(@"Message here!", @"HUD message title");
  179. // Move to bottm center.
  180. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  181. [hud hideAnimated:YES afterDelay:3.f];
  182. }
  183. - (void)cancelationExample {
  184. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  185. // Set the determinate mode to show task progress.
  186. hud.mode = MBProgressHUDModeDeterminate;
  187. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  188. // Configure the button.
  189. [hud.button setTitle:NSLocalizedString(@"Cancel", @"HUD cancel button title") forState:UIControlStateNormal];
  190. [hud.button addTarget:self action:@selector(cancelWork:) forControlEvents:UIControlEventTouchUpInside];
  191. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  192. // Do something useful in the background and update the HUD periodically.
  193. [self doSomeWorkWithProgress];
  194. dispatch_async(dispatch_get_main_queue(), ^{
  195. [hud hideAnimated:YES];
  196. });
  197. });
  198. }
  199. - (void)modeSwitchingExample {
  200. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  201. // Set some text to show the initial status.
  202. hud.label.text = NSLocalizedString(@"Preparing...", @"HUD preparing title");
  203. // Will look best, if we set a minimum size.
  204. hud.minSize = CGSizeMake(150.f, 100.f);
  205. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  206. // Do something useful in the background and update the HUD periodically.
  207. [self doSomeWorkWithMixedProgress];
  208. dispatch_async(dispatch_get_main_queue(), ^{
  209. [hud hideAnimated:YES];
  210. });
  211. });
  212. }
  213. - (void)networkingExample {
  214. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  215. // Set some text to show the initial status.
  216. hud.label.text = NSLocalizedString(@"Preparing...", @"HUD preparing title");
  217. // Will look best, if we set a minimum size.
  218. hud.minSize = CGSizeMake(150.f, 100.f);
  219. [self doSomeNetworkWorkWithProgress];
  220. }
  221. - (void)dimBackgroundExample {
  222. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  223. // Change the background view style and color.
  224. hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
  225. hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.1f];
  226. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  227. [self doSomeWork];
  228. dispatch_async(dispatch_get_main_queue(), ^{
  229. [hud hideAnimated:YES];
  230. });
  231. });
  232. }
  233. - (void)colorExample {
  234. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
  235. hud.contentColor = [UIColor colorWithRed:0.f green:0.6f blue:0.7f alpha:1.f];
  236. // Set the label text.
  237. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  238. dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
  239. [self doSomeWork];
  240. dispatch_async(dispatch_get_main_queue(), ^{
  241. [hud hideAnimated:YES];
  242. });
  243. });
  244. }
  245. #pragma mark - Tasks
  246. - (void)doSomeWork {
  247. // Simulate by just waiting.
  248. sleep(3.);
  249. }
  250. - (void)doSomeWorkWithProgressObject:(NSProgress *)progressObject {
  251. // This just increases the progress indicator in a loop.
  252. while (progressObject.fractionCompleted < 1.0f) {
  253. if (progressObject.isCancelled) break;
  254. [progressObject becomeCurrentWithPendingUnitCount:1];
  255. [progressObject resignCurrent];
  256. usleep(50000);
  257. }
  258. }
  259. - (void)doSomeWorkWithProgress {
  260. self.canceled = NO;
  261. // This just increases the progress indicator in a loop.
  262. float progress = 0.0f;
  263. while (progress < 1.0f) {
  264. if (self.canceled) break;
  265. progress += 0.01f;
  266. dispatch_async(dispatch_get_main_queue(), ^{
  267. // Instead we could have also passed a reference to the HUD
  268. // to the HUD to myProgressTask as a method parameter.
  269. [MBProgressHUD HUDForView:self.navigationController.view].progress = progress;
  270. });
  271. usleep(50000);
  272. }
  273. }
  274. - (void)doSomeWorkWithMixedProgress {
  275. MBProgressHUD *hud = [MBProgressHUD HUDForView:self.navigationController.view];
  276. // Indeterminate mode
  277. sleep(2);
  278. // Switch to determinate mode
  279. dispatch_async(dispatch_get_main_queue(), ^{
  280. hud.mode = MBProgressHUDModeDeterminate;
  281. hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title");
  282. });
  283. float progress = 0.0f;
  284. while (progress < 1.0f) {
  285. progress += 0.01f;
  286. dispatch_async(dispatch_get_main_queue(), ^{
  287. hud.progress = progress;
  288. });
  289. usleep(50000);
  290. }
  291. // Back to indeterminate mode
  292. dispatch_async(dispatch_get_main_queue(), ^{
  293. hud.mode = MBProgressHUDModeIndeterminate;
  294. hud.label.text = NSLocalizedString(@"Cleaning up...", @"HUD cleanining up title");
  295. });
  296. sleep(2);
  297. dispatch_sync(dispatch_get_main_queue(), ^{
  298. UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  299. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  300. hud.customView = imageView;
  301. hud.mode = MBProgressHUDModeCustomView;
  302. hud.label.text = NSLocalizedString(@"Completed", @"HUD completed title");
  303. });
  304. sleep(2);
  305. }
  306. - (void)doSomeNetworkWorkWithProgress {
  307. NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  308. NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
  309. NSURL *URL = [NSURL URLWithString:@"https://support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1425/sample_iPod.m4v.zip"];
  310. NSURLSessionDownloadTask *task = [session downloadTaskWithURL:URL];
  311. [task resume];
  312. }
  313. - (void)cancelWork:(id)sender {
  314. self.canceled = YES;
  315. }
  316. #pragma mark - UITableViewDelegate
  317. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  318. return self.examples.count;
  319. }
  320. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  321. return self.examples[section].count;
  322. }
  323. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  324. MBExample *example = self.examples[indexPath.section][indexPath.row];
  325. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MBExampleCell" forIndexPath:indexPath];
  326. cell.textLabel.text = example.title;
  327. cell.textLabel.textColor = self.view.tintColor;
  328. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  329. cell.selectedBackgroundView = [UIView new];
  330. cell.selectedBackgroundView.backgroundColor = [cell.textLabel.textColor colorWithAlphaComponent:0.1f];
  331. return cell;
  332. }
  333. #pragma mark - UITableViewDelegate
  334. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  335. MBExample *example = self.examples[indexPath.section][indexPath.row];
  336. #pragma clang diagnostic push
  337. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  338. [self performSelector:example.selector];
  339. #pragma clang diagnostic pop
  340. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  341. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  342. });
  343. }
  344. #pragma mark - NSURLSessionDelegate
  345. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
  346. // Do something with the data at location...
  347. // Update the UI on the main thread
  348. dispatch_async(dispatch_get_main_queue(), ^{
  349. MBProgressHUD *hud = [MBProgressHUD HUDForView:self.navigationController.view];
  350. UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  351. UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  352. hud.customView = imageView;
  353. hud.mode = MBProgressHUDModeCustomView;
  354. hud.label.text = NSLocalizedString(@"Completed", @"HUD completed title");
  355. [hud hideAnimated:YES afterDelay:3.f];
  356. });
  357. }
  358. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  359. float progress = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
  360. // Update the UI on the main thread
  361. dispatch_async(dispatch_get_main_queue(), ^{
  362. MBProgressHUD *hud = [MBProgressHUD HUDForView:self.navigationController.view];
  363. hud.mode = MBProgressHUDModeDeterminate;
  364. hud.progress = progress;
  365. });
  366. }
  367. @end