MBProgressHUD.m 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. //
  2. // MBProgressHUD.m
  3. // Version 0.9.2
  4. // Created by Matej Bukovinski on 2.4.09.
  5. //
  6. #import "MBProgressHUD.h"
  7. #import <tgmath.h>
  8. #if __has_feature(objc_arc)
  9. #define MB_AUTORELEASE(exp) exp
  10. #define MB_RELEASE(exp) exp
  11. #define MB_RETAIN(exp) exp
  12. #else
  13. #define MB_AUTORELEASE(exp) [exp autorelease]
  14. #define MB_RELEASE(exp) [exp release]
  15. #define MB_RETAIN(exp) [exp retain]
  16. #endif
  17. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
  18. #define MBLabelAlignmentCenter NSTextAlignmentCenter
  19. #else
  20. #define MBLabelAlignmentCenter UITextAlignmentCenter
  21. #endif
  22. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  23. #define MB_TEXTSIZE(text, font) [text length] > 0 ? [text \
  24. sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero;
  25. #else
  26. #define MB_TEXTSIZE(text, font) [text length] > 0 ? [text sizeWithFont:font] : CGSizeZero;
  27. #endif
  28. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  29. #define MB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \
  30. boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \
  31. attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero;
  32. #else
  33. #define MB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \
  34. sizeWithFont:font constrainedToSize:maxSize lineBreakMode:mode] : CGSizeZero;
  35. #endif
  36. #ifndef kCFCoreFoundationVersionNumber_iOS_7_0
  37. #define kCFCoreFoundationVersionNumber_iOS_7_0 847.20
  38. #endif
  39. #ifndef kCFCoreFoundationVersionNumber_iOS_8_0
  40. #define kCFCoreFoundationVersionNumber_iOS_8_0 1129.15
  41. #endif
  42. static const CGFloat kPadding = 4.f;
  43. static const CGFloat kLabelFontSize = 16.f;
  44. static const CGFloat kDetailsLabelFontSize = 12.f;
  45. @interface MBProgressHUD () {
  46. BOOL useAnimation;
  47. SEL methodForExecution;
  48. id targetForExecution;
  49. id objectForExecution;
  50. UILabel *label;
  51. UILabel *detailsLabel;
  52. BOOL isFinished;
  53. CGAffineTransform rotationTransform;
  54. }
  55. @property (atomic, MB_STRONG) UIView *indicator;
  56. @property (atomic, MB_STRONG) NSTimer *graceTimer;
  57. @property (atomic, MB_STRONG) NSTimer *minShowTimer;
  58. @property (atomic, MB_STRONG) NSDate *showStarted;
  59. @end
  60. @implementation MBProgressHUD
  61. #pragma mark - Properties
  62. @synthesize animationType;
  63. @synthesize delegate;
  64. @synthesize opacity;
  65. @synthesize color;
  66. @synthesize labelFont;
  67. @synthesize labelColor;
  68. @synthesize detailsLabelFont;
  69. @synthesize detailsLabelColor;
  70. @synthesize indicator;
  71. @synthesize xOffset;
  72. @synthesize yOffset;
  73. @synthesize minSize;
  74. @synthesize square;
  75. @synthesize margin;
  76. @synthesize dimBackground;
  77. @synthesize graceTime;
  78. @synthesize minShowTime;
  79. @synthesize graceTimer;
  80. @synthesize minShowTimer;
  81. @synthesize taskInProgress;
  82. @synthesize removeFromSuperViewOnHide;
  83. @synthesize customView;
  84. @synthesize showStarted;
  85. @synthesize mode;
  86. @synthesize labelText;
  87. @synthesize detailsLabelText;
  88. @synthesize progress;
  89. @synthesize size;
  90. @synthesize activityIndicatorColor;
  91. #if NS_BLOCKS_AVAILABLE
  92. @synthesize completionBlock;
  93. #endif
  94. #pragma mark - Class methods
  95. + (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated {
  96. MBProgressHUD *hud = [[self alloc] initWithView:view];
  97. hud.removeFromSuperViewOnHide = YES;
  98. [view addSubview:hud];
  99. [hud show:animated];
  100. return MB_AUTORELEASE(hud);
  101. }
  102. + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
  103. MBProgressHUD *hud = [self HUDForView:view];
  104. if (hud != nil) {
  105. hud.removeFromSuperViewOnHide = YES;
  106. [hud hide:animated];
  107. return YES;
  108. }
  109. return NO;
  110. }
  111. + (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated {
  112. NSArray *huds = [MBProgressHUD allHUDsForView:view];
  113. for (MBProgressHUD *hud in huds) {
  114. hud.removeFromSuperViewOnHide = YES;
  115. [hud hide:animated];
  116. }
  117. return [huds count];
  118. }
  119. + (MB_INSTANCETYPE)HUDForView:(UIView *)view {
  120. NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
  121. for (UIView *subview in subviewsEnum) {
  122. if ([subview isKindOfClass:self]) {
  123. return (MBProgressHUD *)subview;
  124. }
  125. }
  126. return nil;
  127. }
  128. + (NSArray *)allHUDsForView:(UIView *)view {
  129. NSMutableArray *huds = [NSMutableArray array];
  130. NSArray *subviews = view.subviews;
  131. for (UIView *aView in subviews) {
  132. if ([aView isKindOfClass:self]) {
  133. [huds addObject:aView];
  134. }
  135. }
  136. return [NSArray arrayWithArray:huds];
  137. }
  138. #pragma mark - Lifecycle
  139. - (id)initWithFrame:(CGRect)frame {
  140. self = [super initWithFrame:frame];
  141. if (self) {
  142. // Set default values for properties
  143. self.animationType = MBProgressHUDAnimationFade;
  144. self.mode = MBProgressHUDModeIndeterminate;
  145. self.labelText = nil;
  146. self.detailsLabelText = nil;
  147. self.opacity = 0.8f;
  148. self.color = nil;
  149. self.labelFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
  150. self.labelColor = [UIColor whiteColor];
  151. self.detailsLabelFont = [UIFont boldSystemFontOfSize:kDetailsLabelFontSize];
  152. self.detailsLabelColor = [UIColor whiteColor];
  153. self.activityIndicatorColor = [UIColor whiteColor];
  154. self.xOffset = 0.0f;
  155. self.yOffset = 0.0f;
  156. self.dimBackground = NO;
  157. self.margin = 20.0f;
  158. self.cornerRadius = 10.0f;
  159. self.graceTime = 0.0f;
  160. self.minShowTime = 0.0f;
  161. self.removeFromSuperViewOnHide = NO;
  162. self.minSize = CGSizeZero;
  163. self.square = NO;
  164. self.contentMode = UIViewContentModeCenter;
  165. self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
  166. | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  167. // Transparent background
  168. self.opaque = NO;
  169. self.backgroundColor = [UIColor clearColor];
  170. // Make it invisible for now
  171. self.alpha = 0.0f;
  172. taskInProgress = NO;
  173. rotationTransform = CGAffineTransformIdentity;
  174. [self setupLabels];
  175. [self updateIndicators];
  176. [self registerForKVO];
  177. [self registerForNotifications];
  178. }
  179. return self;
  180. }
  181. - (id)initWithView:(UIView *)view {
  182. NSAssert(view, @"View must not be nil.");
  183. return [self initWithFrame:view.bounds];
  184. }
  185. - (id)initWithWindow:(UIWindow *)window {
  186. return [self initWithView:window];
  187. }
  188. - (void)dealloc {
  189. [self unregisterFromNotifications];
  190. [self unregisterFromKVO];
  191. #if !__has_feature(objc_arc)
  192. [color release];
  193. [indicator release];
  194. [label release];
  195. [detailsLabel release];
  196. [labelText release];
  197. [detailsLabelText release];
  198. [graceTimer release];
  199. [minShowTimer release];
  200. [showStarted release];
  201. [customView release];
  202. [labelFont release];
  203. [labelColor release];
  204. [detailsLabelFont release];
  205. [detailsLabelColor release];
  206. #if NS_BLOCKS_AVAILABLE
  207. [completionBlock release];
  208. #endif
  209. [super dealloc];
  210. #endif
  211. }
  212. #pragma mark - Show & hide
  213. - (void)show:(BOOL)animated {
  214. NSAssert([NSThread isMainThread], @"MBProgressHUD needs to be accessed on the main thread.");
  215. useAnimation = animated;
  216. // If the grace time is set postpone the HUD display
  217. if (self.graceTime > 0.0) {
  218. NSTimer *newGraceTimer = [NSTimer timerWithTimeInterval:self.graceTime target:self selector:@selector(handleGraceTimer:) userInfo:nil repeats:NO];
  219. [[NSRunLoop currentRunLoop] addTimer:newGraceTimer forMode:NSRunLoopCommonModes];
  220. self.graceTimer = newGraceTimer;
  221. }
  222. // ... otherwise show the HUD imediately
  223. else {
  224. [self showUsingAnimation:useAnimation];
  225. }
  226. }
  227. - (void)hide:(BOOL)animated {
  228. NSAssert([NSThread isMainThread], @"MBProgressHUD needs to be accessed on the main thread.");
  229. useAnimation = animated;
  230. // If the minShow time is set, calculate how long the hud was shown,
  231. // and pospone the hiding operation if necessary
  232. if (self.minShowTime > 0.0 && showStarted) {
  233. NSTimeInterval interv = [[NSDate date] timeIntervalSinceDate:showStarted];
  234. if (interv < self.minShowTime) {
  235. self.minShowTimer = [NSTimer scheduledTimerWithTimeInterval:(self.minShowTime - interv) target:self
  236. selector:@selector(handleMinShowTimer:) userInfo:nil repeats:NO];
  237. return;
  238. }
  239. }
  240. // ... otherwise hide the HUD immediately
  241. [self hideUsingAnimation:useAnimation];
  242. }
  243. - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay {
  244. [self performSelector:@selector(hideDelayed:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay];
  245. }
  246. - (void)hideDelayed:(NSNumber *)animated {
  247. [self hide:[animated boolValue]];
  248. }
  249. #pragma mark - Timer callbacks
  250. - (void)handleGraceTimer:(NSTimer *)theTimer {
  251. // Show the HUD only if the task is still running
  252. if (taskInProgress) {
  253. [self showUsingAnimation:useAnimation];
  254. }
  255. }
  256. - (void)handleMinShowTimer:(NSTimer *)theTimer {
  257. [self hideUsingAnimation:useAnimation];
  258. }
  259. #pragma mark - View Hierrarchy
  260. - (void)didMoveToSuperview {
  261. [self updateForCurrentOrientationAnimated:NO];
  262. }
  263. #pragma mark - Internal show & hide operations
  264. - (void)showUsingAnimation:(BOOL)animated {
  265. // Cancel any scheduled hideDelayed: calls
  266. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  267. [self setNeedsDisplay];
  268. if (animated && animationType == MBProgressHUDAnimationZoomIn) {
  269. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));
  270. } else if (animated && animationType == MBProgressHUDAnimationZoomOut) {
  271. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f));
  272. }
  273. self.showStarted = [NSDate date];
  274. // Fade in
  275. if (animated) {
  276. [UIView beginAnimations:nil context:NULL];
  277. [UIView setAnimationDuration:0.30];
  278. self.alpha = 1.0f;
  279. if (animationType == MBProgressHUDAnimationZoomIn || animationType == MBProgressHUDAnimationZoomOut) {
  280. self.transform = rotationTransform;
  281. }
  282. [UIView commitAnimations];
  283. }
  284. else {
  285. self.alpha = 1.0f;
  286. }
  287. }
  288. - (void)hideUsingAnimation:(BOOL)animated {
  289. // Fade out
  290. if (animated && showStarted) {
  291. [UIView beginAnimations:nil context:NULL];
  292. [UIView setAnimationDuration:0.30];
  293. [UIView setAnimationDelegate:self];
  294. [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
  295. // 0.02 prevents the hud from passing through touches during the animation the hud will get completely hidden
  296. // in the done method
  297. if (animationType == MBProgressHUDAnimationZoomIn) {
  298. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f));
  299. } else if (animationType == MBProgressHUDAnimationZoomOut) {
  300. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));
  301. }
  302. self.alpha = 0.02f;
  303. [UIView commitAnimations];
  304. }
  305. else {
  306. self.alpha = 0.0f;
  307. [self done];
  308. }
  309. self.showStarted = nil;
  310. }
  311. - (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void*)context {
  312. [self done];
  313. }
  314. - (void)done {
  315. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  316. isFinished = YES;
  317. self.alpha = 0.0f;
  318. if (removeFromSuperViewOnHide) {
  319. [self removeFromSuperview];
  320. }
  321. #if NS_BLOCKS_AVAILABLE
  322. if (self.completionBlock) {
  323. self.completionBlock();
  324. self.completionBlock = NULL;
  325. }
  326. #endif
  327. if ([delegate respondsToSelector:@selector(hudWasHidden:)]) {
  328. [delegate performSelector:@selector(hudWasHidden:) withObject:self];
  329. }
  330. }
  331. #pragma mark - Threading
  332. - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {
  333. methodForExecution = method;
  334. targetForExecution = MB_RETAIN(target);
  335. objectForExecution = MB_RETAIN(object);
  336. // Launch execution in new thread
  337. self.taskInProgress = YES;
  338. [NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil];
  339. // Show HUD view
  340. [self show:animated];
  341. }
  342. #if NS_BLOCKS_AVAILABLE
  343. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block {
  344. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  345. [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:NULL];
  346. }
  347. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(void (^)())completion {
  348. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  349. [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:completion];
  350. }
  351. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue {
  352. [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:NULL];
  353. }
  354. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue
  355. completionBlock:(MBProgressHUDCompletionBlock)completion {
  356. self.taskInProgress = YES;
  357. self.completionBlock = completion;
  358. dispatch_async(queue, ^(void) {
  359. block();
  360. dispatch_async(dispatch_get_main_queue(), ^(void) {
  361. [self cleanUp];
  362. });
  363. });
  364. [self show:animated];
  365. }
  366. #endif
  367. - (void)launchExecution {
  368. @autoreleasepool {
  369. #pragma clang diagnostic push
  370. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  371. // Start executing the requested task
  372. [targetForExecution performSelector:methodForExecution withObject:objectForExecution];
  373. #pragma clang diagnostic pop
  374. // Task completed, update view in main thread (note: view operations should
  375. // be done only in the main thread)
  376. [self performSelectorOnMainThread:@selector(cleanUp) withObject:nil waitUntilDone:NO];
  377. }
  378. }
  379. - (void)cleanUp {
  380. taskInProgress = NO;
  381. #if !__has_feature(objc_arc)
  382. [targetForExecution release];
  383. [objectForExecution release];
  384. #else
  385. targetForExecution = nil;
  386. objectForExecution = nil;
  387. #endif
  388. [self hide:useAnimation];
  389. }
  390. #pragma mark - UI
  391. - (void)setupLabels {
  392. label = [[UILabel alloc] initWithFrame:self.bounds];
  393. label.adjustsFontSizeToFitWidth = NO;
  394. label.textAlignment = MBLabelAlignmentCenter;
  395. label.opaque = NO;
  396. label.backgroundColor = [UIColor clearColor];
  397. label.textColor = self.labelColor;
  398. label.font = self.labelFont;
  399. label.text = self.labelText;
  400. [self addSubview:label];
  401. detailsLabel = [[UILabel alloc] initWithFrame:self.bounds];
  402. detailsLabel.font = self.detailsLabelFont;
  403. detailsLabel.adjustsFontSizeToFitWidth = NO;
  404. detailsLabel.textAlignment = MBLabelAlignmentCenter;
  405. detailsLabel.opaque = NO;
  406. detailsLabel.backgroundColor = [UIColor clearColor];
  407. detailsLabel.textColor = self.detailsLabelColor;
  408. detailsLabel.numberOfLines = 0;
  409. detailsLabel.font = self.detailsLabelFont;
  410. detailsLabel.text = self.detailsLabelText;
  411. [self addSubview:detailsLabel];
  412. }
  413. - (void)updateIndicators {
  414. BOOL isActivityIndicator = [indicator isKindOfClass:[UIActivityIndicatorView class]];
  415. BOOL isRoundIndicator = [indicator isKindOfClass:[MBRoundProgressView class]];
  416. if (mode == MBProgressHUDModeIndeterminate) {
  417. if (!isActivityIndicator) {
  418. // Update to indeterminate indicator
  419. [indicator removeFromSuperview];
  420. self.indicator = MB_AUTORELEASE([[UIActivityIndicatorView alloc]
  421. initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]);
  422. [(UIActivityIndicatorView *)indicator startAnimating];
  423. [self addSubview:indicator];
  424. }
  425. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000
  426. [(UIActivityIndicatorView *)indicator setColor:self.activityIndicatorColor];
  427. #endif
  428. }
  429. else if (mode == MBProgressHUDModeDeterminateHorizontalBar) {
  430. // Update to bar determinate indicator
  431. [indicator removeFromSuperview];
  432. self.indicator = MB_AUTORELEASE([[MBBarProgressView alloc] init]);
  433. [self addSubview:indicator];
  434. }
  435. else if (mode == MBProgressHUDModeDeterminate || mode == MBProgressHUDModeAnnularDeterminate) {
  436. if (!isRoundIndicator) {
  437. // Update to determinante indicator
  438. [indicator removeFromSuperview];
  439. self.indicator = MB_AUTORELEASE([[MBRoundProgressView alloc] init]);
  440. [self addSubview:indicator];
  441. }
  442. if (mode == MBProgressHUDModeAnnularDeterminate) {
  443. [(MBRoundProgressView *)indicator setAnnular:YES];
  444. }
  445. [(MBRoundProgressView *)indicator setProgressTintColor:self.activityIndicatorColor];
  446. [(MBRoundProgressView *)indicator setBackgroundTintColor:[self.activityIndicatorColor colorWithAlphaComponent:0.1f]];
  447. }
  448. else if (mode == MBProgressHUDModeCustomView && customView != indicator) {
  449. // Update custom view indicator
  450. [indicator removeFromSuperview];
  451. self.indicator = customView;
  452. [self addSubview:indicator];
  453. } else if (mode == MBProgressHUDModeText) {
  454. [indicator removeFromSuperview];
  455. self.indicator = nil;
  456. }
  457. }
  458. #pragma mark - Layout
  459. - (void)layoutSubviews {
  460. [super layoutSubviews];
  461. // Entirely cover the parent view
  462. UIView *parent = self.superview;
  463. if (parent) {
  464. self.frame = parent.bounds;
  465. }
  466. CGRect bounds = self.bounds;
  467. // Determine the total width and height needed
  468. CGFloat maxWidth = bounds.size.width - 4 * margin;
  469. CGSize totalSize = CGSizeZero;
  470. CGRect indicatorF = indicator.bounds;
  471. indicatorF.size.width = MIN(indicatorF.size.width, maxWidth);
  472. totalSize.width = MAX(totalSize.width, indicatorF.size.width);
  473. totalSize.height += indicatorF.size.height;
  474. CGSize labelSize = MB_TEXTSIZE(label.text, label.font);
  475. labelSize.width = MIN(labelSize.width, maxWidth);
  476. totalSize.width = MAX(totalSize.width, labelSize.width);
  477. totalSize.height += labelSize.height;
  478. if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
  479. totalSize.height += kPadding;
  480. }
  481. CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
  482. CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
  483. CGSize detailsLabelSize = MB_MULTILINE_TEXTSIZE(detailsLabel.text, detailsLabel.font, maxSize, detailsLabel.lineBreakMode);
  484. totalSize.width = MAX(totalSize.width, detailsLabelSize.width);
  485. totalSize.height += detailsLabelSize.height;
  486. if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {
  487. totalSize.height += kPadding;
  488. }
  489. totalSize.width += 2 * margin;
  490. totalSize.height += 2 * margin;
  491. // Position elements
  492. CGFloat yPos = round(((bounds.size.height - totalSize.height) / 2)) + margin + yOffset;
  493. CGFloat xPos = xOffset;
  494. indicatorF.origin.y = yPos;
  495. indicatorF.origin.x = round((bounds.size.width - indicatorF.size.width) / 2) + xPos;
  496. indicator.frame = indicatorF;
  497. yPos += indicatorF.size.height;
  498. if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
  499. yPos += kPadding;
  500. }
  501. CGRect labelF;
  502. labelF.origin.y = yPos;
  503. labelF.origin.x = round((bounds.size.width - labelSize.width) / 2) + xPos;
  504. labelF.size = labelSize;
  505. label.frame = labelF;
  506. yPos += labelF.size.height;
  507. if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {
  508. yPos += kPadding;
  509. }
  510. CGRect detailsLabelF;
  511. detailsLabelF.origin.y = yPos;
  512. detailsLabelF.origin.x = round((bounds.size.width - detailsLabelSize.width) / 2) + xPos;
  513. detailsLabelF.size = detailsLabelSize;
  514. detailsLabel.frame = detailsLabelF;
  515. // Enforce minsize and quare rules
  516. if (square) {
  517. CGFloat max = MAX(totalSize.width, totalSize.height);
  518. if (max <= bounds.size.width - 2 * margin) {
  519. totalSize.width = max;
  520. }
  521. if (max <= bounds.size.height - 2 * margin) {
  522. totalSize.height = max;
  523. }
  524. }
  525. if (totalSize.width < minSize.width) {
  526. totalSize.width = minSize.width;
  527. }
  528. if (totalSize.height < minSize.height) {
  529. totalSize.height = minSize.height;
  530. }
  531. size = totalSize;
  532. }
  533. #pragma mark BG Drawing
  534. - (void)drawRect:(CGRect)rect {
  535. CGContextRef context = UIGraphicsGetCurrentContext();
  536. UIGraphicsPushContext(context);
  537. if (self.dimBackground) {
  538. //Gradient colours
  539. size_t gradLocationsNum = 2;
  540. CGFloat gradLocations[2] = {0.0f, 1.0f};
  541. CGFloat gradColors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f};
  542. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  543. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
  544. CGColorSpaceRelease(colorSpace);
  545. //Gradient center
  546. CGPoint gradCenter= CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  547. //Gradient radius
  548. float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ;
  549. //Gradient draw
  550. CGContextDrawRadialGradient (context, gradient, gradCenter,
  551. 0, gradCenter, gradRadius,
  552. kCGGradientDrawsAfterEndLocation);
  553. CGGradientRelease(gradient);
  554. }
  555. // Set background rect color
  556. if (self.color) {
  557. CGContextSetFillColorWithColor(context, self.color.CGColor);
  558. } else {
  559. CGContextSetGrayFillColor(context, 0.0f, self.opacity);
  560. }
  561. // Center HUD
  562. CGRect allRect = self.bounds;
  563. // Draw rounded HUD backgroud rect
  564. CGRect boxRect = CGRectMake(round((allRect.size.width - size.width) / 2) + self.xOffset,
  565. round((allRect.size.height - size.height) / 2) + self.yOffset, size.width, size.height);
  566. float radius = self.cornerRadius;
  567. CGContextBeginPath(context);
  568. CGContextMoveToPoint(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));
  569. CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius, 3 * (float)M_PI / 2, 0, 0);
  570. CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius, 0, (float)M_PI / 2, 0);
  571. CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius, (float)M_PI / 2, (float)M_PI, 0);
  572. CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI, 3 * (float)M_PI / 2, 0);
  573. CGContextClosePath(context);
  574. CGContextFillPath(context);
  575. UIGraphicsPopContext();
  576. }
  577. #pragma mark - KVO
  578. - (void)registerForKVO {
  579. for (NSString *keyPath in [self observableKeypaths]) {
  580. [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
  581. }
  582. }
  583. - (void)unregisterFromKVO {
  584. for (NSString *keyPath in [self observableKeypaths]) {
  585. [self removeObserver:self forKeyPath:keyPath];
  586. }
  587. }
  588. - (NSArray *)observableKeypaths {
  589. return [NSArray arrayWithObjects:@"mode", @"customView", @"labelText", @"labelFont", @"labelColor",
  590. @"detailsLabelText", @"detailsLabelFont", @"detailsLabelColor", @"progress", @"activityIndicatorColor", nil];
  591. }
  592. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  593. if (![NSThread isMainThread]) {
  594. [self performSelectorOnMainThread:@selector(updateUIForKeypath:) withObject:keyPath waitUntilDone:NO];
  595. } else {
  596. [self updateUIForKeypath:keyPath];
  597. }
  598. }
  599. - (void)updateUIForKeypath:(NSString *)keyPath {
  600. if ([keyPath isEqualToString:@"mode"] || [keyPath isEqualToString:@"customView"] ||
  601. [keyPath isEqualToString:@"activityIndicatorColor"]) {
  602. [self updateIndicators];
  603. } else if ([keyPath isEqualToString:@"labelText"]) {
  604. label.text = self.labelText;
  605. } else if ([keyPath isEqualToString:@"labelFont"]) {
  606. label.font = self.labelFont;
  607. } else if ([keyPath isEqualToString:@"labelColor"]) {
  608. label.textColor = self.labelColor;
  609. } else if ([keyPath isEqualToString:@"detailsLabelText"]) {
  610. detailsLabel.text = self.detailsLabelText;
  611. } else if ([keyPath isEqualToString:@"detailsLabelFont"]) {
  612. detailsLabel.font = self.detailsLabelFont;
  613. } else if ([keyPath isEqualToString:@"detailsLabelColor"]) {
  614. detailsLabel.textColor = self.detailsLabelColor;
  615. } else if ([keyPath isEqualToString:@"progress"]) {
  616. if ([indicator respondsToSelector:@selector(setProgress:)]) {
  617. [(id)indicator setValue:@(progress) forKey:@"progress"];
  618. }
  619. return;
  620. }
  621. [self setNeedsLayout];
  622. [self setNeedsDisplay];
  623. }
  624. #pragma mark - Notifications
  625. - (void)registerForNotifications {
  626. #if !TARGET_OS_TV
  627. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  628. [nc addObserver:self selector:@selector(statusBarOrientationDidChange:)
  629. name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  630. #endif
  631. }
  632. - (void)unregisterFromNotifications {
  633. #if !TARGET_OS_TV
  634. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  635. [nc removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  636. #endif
  637. }
  638. #if !TARGET_OS_TV
  639. - (void)statusBarOrientationDidChange:(NSNotification *)notification {
  640. UIView *superview = self.superview;
  641. if (!superview) {
  642. return;
  643. } else {
  644. [self updateForCurrentOrientationAnimated:YES];
  645. }
  646. }
  647. #endif
  648. - (void)updateForCurrentOrientationAnimated:(BOOL)animated {
  649. // Stay in sync with the superview in any case
  650. if (self.superview) {
  651. self.bounds = self.superview.bounds;
  652. [self setNeedsDisplay];
  653. }
  654. // Not needed on iOS 8+, compile out when the deployment target allows,
  655. // to avoid sharedApplication problems on extension targets
  656. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
  657. // Only needed pre iOS 7 when added to a window
  658. BOOL iOS8OrLater = kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_8_0;
  659. if (iOS8OrLater || ![self.superview isKindOfClass:[UIWindow class]]) return;
  660. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  661. CGFloat radians = 0;
  662. if (UIInterfaceOrientationIsLandscape(orientation)) {
  663. if (orientation == UIInterfaceOrientationLandscapeLeft) { radians = -(CGFloat)M_PI_2; }
  664. else { radians = (CGFloat)M_PI_2; }
  665. // Window coordinates differ!
  666. self.bounds = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.width);
  667. } else {
  668. if (orientation == UIInterfaceOrientationPortraitUpsideDown) { radians = (CGFloat)M_PI; }
  669. else { radians = 0; }
  670. }
  671. rotationTransform = CGAffineTransformMakeRotation(radians);
  672. if (animated) {
  673. [UIView beginAnimations:nil context:nil];
  674. [UIView setAnimationDuration:0.3];
  675. }
  676. [self setTransform:rotationTransform];
  677. if (animated) {
  678. [UIView commitAnimations];
  679. }
  680. #endif
  681. }
  682. @end
  683. @implementation MBRoundProgressView
  684. #pragma mark - Lifecycle
  685. - (id)init {
  686. return [self initWithFrame:CGRectMake(0.f, 0.f, 37.f, 37.f)];
  687. }
  688. - (id)initWithFrame:(CGRect)frame {
  689. self = [super initWithFrame:frame];
  690. if (self) {
  691. self.backgroundColor = [UIColor clearColor];
  692. self.opaque = NO;
  693. _progress = 0.f;
  694. _annular = NO;
  695. _progressTintColor = [[UIColor alloc] initWithWhite:1.f alpha:1.f];
  696. _backgroundTintColor = [[UIColor alloc] initWithWhite:1.f alpha:.1f];
  697. [self registerForKVO];
  698. }
  699. return self;
  700. }
  701. - (void)dealloc {
  702. [self unregisterFromKVO];
  703. #if !__has_feature(objc_arc)
  704. [_progressTintColor release];
  705. [_backgroundTintColor release];
  706. [super dealloc];
  707. #endif
  708. }
  709. #pragma mark - Drawing
  710. - (void)drawRect:(CGRect)rect {
  711. CGRect allRect = self.bounds;
  712. CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);
  713. CGContextRef context = UIGraphicsGetCurrentContext();
  714. if (_annular) {
  715. // Draw background
  716. BOOL isPreiOS7 = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0;
  717. CGFloat lineWidth = isPreiOS7 ? 5.f : 2.f;
  718. UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
  719. processBackgroundPath.lineWidth = lineWidth;
  720. processBackgroundPath.lineCapStyle = kCGLineCapButt;
  721. CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  722. CGFloat radius = (self.bounds.size.width - lineWidth)/2;
  723. CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
  724. CGFloat endAngle = (2 * (float)M_PI) + startAngle;
  725. [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  726. [_backgroundTintColor set];
  727. [processBackgroundPath stroke];
  728. // Draw progress
  729. UIBezierPath *processPath = [UIBezierPath bezierPath];
  730. processPath.lineCapStyle = isPreiOS7 ? kCGLineCapRound : kCGLineCapSquare;
  731. processPath.lineWidth = lineWidth;
  732. endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
  733. [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  734. [_progressTintColor set];
  735. [processPath stroke];
  736. } else {
  737. // Draw background
  738. [_progressTintColor setStroke];
  739. [_backgroundTintColor setFill];
  740. CGContextSetLineWidth(context, 2.0f);
  741. CGContextFillEllipseInRect(context, circleRect);
  742. CGContextStrokeEllipseInRect(context, circleRect);
  743. // Draw progress
  744. CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
  745. CGFloat radius = (allRect.size.width - 4) / 2;
  746. CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
  747. CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
  748. [_progressTintColor setFill];
  749. CGContextMoveToPoint(context, center.x, center.y);
  750. CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
  751. CGContextClosePath(context);
  752. CGContextFillPath(context);
  753. }
  754. }
  755. #pragma mark - KVO
  756. - (void)registerForKVO {
  757. for (NSString *keyPath in [self observableKeypaths]) {
  758. [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
  759. }
  760. }
  761. - (void)unregisterFromKVO {
  762. for (NSString *keyPath in [self observableKeypaths]) {
  763. [self removeObserver:self forKeyPath:keyPath];
  764. }
  765. }
  766. - (NSArray *)observableKeypaths {
  767. return [NSArray arrayWithObjects:@"progressTintColor", @"backgroundTintColor", @"progress", @"annular", nil];
  768. }
  769. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  770. [self setNeedsDisplay];
  771. }
  772. @end
  773. @implementation MBBarProgressView
  774. #pragma mark - Lifecycle
  775. - (id)init {
  776. return [self initWithFrame:CGRectMake(.0f, .0f, 120.0f, 20.0f)];
  777. }
  778. - (id)initWithFrame:(CGRect)frame {
  779. self = [super initWithFrame:frame];
  780. if (self) {
  781. _progress = 0.f;
  782. _lineColor = [UIColor whiteColor];
  783. _progressColor = [UIColor whiteColor];
  784. _progressRemainingColor = [UIColor clearColor];
  785. self.backgroundColor = [UIColor clearColor];
  786. self.opaque = NO;
  787. [self registerForKVO];
  788. }
  789. return self;
  790. }
  791. - (void)dealloc {
  792. [self unregisterFromKVO];
  793. #if !__has_feature(objc_arc)
  794. [_lineColor release];
  795. [_progressColor release];
  796. [_progressRemainingColor release];
  797. [super dealloc];
  798. #endif
  799. }
  800. #pragma mark - Drawing
  801. - (void)drawRect:(CGRect)rect {
  802. CGContextRef context = UIGraphicsGetCurrentContext();
  803. CGContextSetLineWidth(context, 2);
  804. CGContextSetStrokeColorWithColor(context,[_lineColor CGColor]);
  805. CGContextSetFillColorWithColor(context, [_progressRemainingColor CGColor]);
  806. // Draw background
  807. float radius = (rect.size.height / 2) - 2;
  808. CGContextMoveToPoint(context, 2, rect.size.height/2);
  809. CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
  810. CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
  811. CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
  812. CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
  813. CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
  814. CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
  815. CGContextFillPath(context);
  816. // Draw border
  817. CGContextMoveToPoint(context, 2, rect.size.height/2);
  818. CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
  819. CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
  820. CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
  821. CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
  822. CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
  823. CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
  824. CGContextStrokePath(context);
  825. CGContextSetFillColorWithColor(context, [_progressColor CGColor]);
  826. radius = radius - 2;
  827. float amount = self.progress * rect.size.width;
  828. // Progress in the middle area
  829. if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4)) {
  830. CGContextMoveToPoint(context, 4, rect.size.height/2);
  831. CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
  832. CGContextAddLineToPoint(context, amount, 4);
  833. CGContextAddLineToPoint(context, amount, radius + 4);
  834. CGContextMoveToPoint(context, 4, rect.size.height/2);
  835. CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
  836. CGContextAddLineToPoint(context, amount, rect.size.height - 4);
  837. CGContextAddLineToPoint(context, amount, radius + 4);
  838. CGContextFillPath(context);
  839. }
  840. // Progress in the right arc
  841. else if (amount > radius + 4) {
  842. float x = amount - (rect.size.width - radius - 4);
  843. CGContextMoveToPoint(context, 4, rect.size.height/2);
  844. CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
  845. CGContextAddLineToPoint(context, rect.size.width - radius - 4, 4);
  846. float angle = -acos(x/radius);
  847. if (isnan(angle)) angle = 0;
  848. CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, M_PI, angle, 0);
  849. CGContextAddLineToPoint(context, amount, rect.size.height/2);
  850. CGContextMoveToPoint(context, 4, rect.size.height/2);
  851. CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
  852. CGContextAddLineToPoint(context, rect.size.width - radius - 4, rect.size.height - 4);
  853. angle = acos(x/radius);
  854. if (isnan(angle)) angle = 0;
  855. CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, -M_PI, angle, 1);
  856. CGContextAddLineToPoint(context, amount, rect.size.height/2);
  857. CGContextFillPath(context);
  858. }
  859. // Progress is in the left arc
  860. else if (amount < radius + 4 && amount > 0) {
  861. CGContextMoveToPoint(context, 4, rect.size.height/2);
  862. CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
  863. CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
  864. CGContextMoveToPoint(context, 4, rect.size.height/2);
  865. CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
  866. CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
  867. CGContextFillPath(context);
  868. }
  869. }
  870. #pragma mark - KVO
  871. - (void)registerForKVO {
  872. for (NSString *keyPath in [self observableKeypaths]) {
  873. [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
  874. }
  875. }
  876. - (void)unregisterFromKVO {
  877. for (NSString *keyPath in [self observableKeypaths]) {
  878. [self removeObserver:self forKeyPath:keyPath];
  879. }
  880. }
  881. - (NSArray *)observableKeypaths {
  882. return [NSArray arrayWithObjects:@"lineColor", @"progressRemainingColor", @"progressColor", @"progress", nil];
  883. }
  884. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  885. [self setNeedsDisplay];
  886. }
  887. @end