PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/MBProgressHUD/MBProgressHUD.m

https://gitlab.com/Mr.Tomato/VideoEffects
Objective C | 1024 lines | 828 code | 146 blank | 50 comment | 134 complexity | acaead7bdf2e2e1c5f24b23f1099d2a9 MD5 | raw file
  1. //
  2. // MBProgressHUD.m
  3. // Version 0.9
  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. useAnimation = animated;
  215. // If the grace time is set postpone the HUD display
  216. if (self.graceTime > 0.0) {
  217. self.graceTimer = [NSTimer scheduledTimerWithTimeInterval:self.graceTime target:self
  218. selector:@selector(handleGraceTimer:) userInfo:nil repeats:NO];
  219. }
  220. // ... otherwise show the HUD imediately
  221. else {
  222. [self setNeedsDisplay];
  223. [self showUsingAnimation:useAnimation];
  224. }
  225. }
  226. - (void)hide:(BOOL)animated {
  227. useAnimation = animated;
  228. // If the minShow time is set, calculate how long the hud was shown,
  229. // and pospone the hiding operation if necessary
  230. if (self.minShowTime > 0.0 && showStarted) {
  231. NSTimeInterval interv = [[NSDate date] timeIntervalSinceDate:showStarted];
  232. if (interv < self.minShowTime) {
  233. self.minShowTimer = [NSTimer scheduledTimerWithTimeInterval:(self.minShowTime - interv) target:self
  234. selector:@selector(handleMinShowTimer:) userInfo:nil repeats:NO];
  235. return;
  236. }
  237. }
  238. // ... otherwise hide the HUD immediately
  239. [self hideUsingAnimation:useAnimation];
  240. }
  241. - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay {
  242. [self performSelector:@selector(hideDelayed:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay];
  243. }
  244. - (void)hideDelayed:(NSNumber *)animated {
  245. [self hide:[animated boolValue]];
  246. }
  247. #pragma mark - Timer callbacks
  248. - (void)handleGraceTimer:(NSTimer *)theTimer {
  249. // Show the HUD only if the task is still running
  250. if (taskInProgress) {
  251. [self setNeedsDisplay];
  252. [self showUsingAnimation:useAnimation];
  253. }
  254. }
  255. - (void)handleMinShowTimer:(NSTimer *)theTimer {
  256. [self hideUsingAnimation:useAnimation];
  257. }
  258. #pragma mark - View Hierrarchy
  259. - (BOOL)shouldPerformOrientationTransform {
  260. BOOL isPreiOS8 = NSFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_8_0;
  261. // prior to iOS8 code needs to take care of rotation if it is being added to the window
  262. return isPreiOS8 && [self.superview isKindOfClass:[UIWindow class]];
  263. }
  264. - (void)didMoveToSuperview {
  265. if ([self shouldPerformOrientationTransform]) {
  266. [self setTransformForCurrentOrientation:NO];
  267. }
  268. }
  269. #pragma mark - Internal show & hide operations
  270. - (void)showUsingAnimation:(BOOL)animated {
  271. if (animated && animationType == MBProgressHUDAnimationZoomIn) {
  272. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));
  273. } else if (animated && animationType == MBProgressHUDAnimationZoomOut) {
  274. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f));
  275. }
  276. self.showStarted = [NSDate date];
  277. // Fade in
  278. if (animated) {
  279. [UIView beginAnimations:nil context:NULL];
  280. [UIView setAnimationDuration:0.30];
  281. self.alpha = 1.0f;
  282. if (animationType == MBProgressHUDAnimationZoomIn || animationType == MBProgressHUDAnimationZoomOut) {
  283. self.transform = rotationTransform;
  284. }
  285. [UIView commitAnimations];
  286. }
  287. else {
  288. self.alpha = 1.0f;
  289. }
  290. }
  291. - (void)hideUsingAnimation:(BOOL)animated {
  292. // Fade out
  293. if (animated && showStarted) {
  294. [UIView beginAnimations:nil context:NULL];
  295. [UIView setAnimationDuration:0.30];
  296. [UIView setAnimationDelegate:self];
  297. [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
  298. // 0.02 prevents the hud from passing through touches during the animation the hud will get completely hidden
  299. // in the done method
  300. if (animationType == MBProgressHUDAnimationZoomIn) {
  301. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f));
  302. } else if (animationType == MBProgressHUDAnimationZoomOut) {
  303. self.transform = CGAffineTransformConcat(rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));
  304. }
  305. self.alpha = 0.02f;
  306. [UIView commitAnimations];
  307. }
  308. else {
  309. self.alpha = 0.0f;
  310. [self done];
  311. }
  312. self.showStarted = nil;
  313. }
  314. - (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void*)context {
  315. [self done];
  316. }
  317. - (void)done {
  318. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  319. isFinished = YES;
  320. self.alpha = 0.0f;
  321. if (removeFromSuperViewOnHide) {
  322. [self removeFromSuperview];
  323. }
  324. #if NS_BLOCKS_AVAILABLE
  325. if (self.completionBlock) {
  326. self.completionBlock();
  327. self.completionBlock = NULL;
  328. }
  329. #endif
  330. if ([delegate respondsToSelector:@selector(hudWasHidden:)]) {
  331. [delegate performSelector:@selector(hudWasHidden:) withObject:self];
  332. }
  333. }
  334. #pragma mark - Threading
  335. - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {
  336. methodForExecution = method;
  337. targetForExecution = MB_RETAIN(target);
  338. objectForExecution = MB_RETAIN(object);
  339. // Launch execution in new thread
  340. self.taskInProgress = YES;
  341. [NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil];
  342. // Show HUD view
  343. [self show:animated];
  344. }
  345. #if NS_BLOCKS_AVAILABLE
  346. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block {
  347. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  348. [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:NULL];
  349. }
  350. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(void (^)())completion {
  351. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  352. [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:completion];
  353. }
  354. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue {
  355. [self showAnimated:animated whileExecutingBlock:block onQueue:queue completionBlock:NULL];
  356. }
  357. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue
  358. completionBlock:(MBProgressHUDCompletionBlock)completion {
  359. self.taskInProgress = YES;
  360. self.completionBlock = completion;
  361. dispatch_async(queue, ^(void) {
  362. block();
  363. dispatch_async(dispatch_get_main_queue(), ^(void) {
  364. [self cleanUp];
  365. });
  366. });
  367. [self show:animated];
  368. }
  369. #endif
  370. - (void)launchExecution {
  371. @autoreleasepool {
  372. #pragma clang diagnostic push
  373. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  374. // Start executing the requested task
  375. [targetForExecution performSelector:methodForExecution withObject:objectForExecution];
  376. #pragma clang diagnostic pop
  377. // Task completed, update view in main thread (note: view operations should
  378. // be done only in the main thread)
  379. [self performSelectorOnMainThread:@selector(cleanUp) withObject:nil waitUntilDone:NO];
  380. }
  381. }
  382. - (void)cleanUp {
  383. taskInProgress = NO;
  384. #if !__has_feature(objc_arc)
  385. [targetForExecution release];
  386. [objectForExecution release];
  387. #else
  388. targetForExecution = nil;
  389. objectForExecution = nil;
  390. #endif
  391. [self hide:useAnimation];
  392. }
  393. #pragma mark - UI
  394. - (void)setupLabels {
  395. label = [[UILabel alloc] initWithFrame:self.bounds];
  396. label.adjustsFontSizeToFitWidth = NO;
  397. label.textAlignment = MBLabelAlignmentCenter;
  398. label.opaque = NO;
  399. label.backgroundColor = [UIColor clearColor];
  400. label.textColor = self.labelColor;
  401. label.font = self.labelFont;
  402. label.text = self.labelText;
  403. [self addSubview:label];
  404. detailsLabel = [[UILabel alloc] initWithFrame:self.bounds];
  405. detailsLabel.font = self.detailsLabelFont;
  406. detailsLabel.adjustsFontSizeToFitWidth = NO;
  407. detailsLabel.textAlignment = MBLabelAlignmentCenter;
  408. detailsLabel.opaque = NO;
  409. detailsLabel.backgroundColor = [UIColor clearColor];
  410. detailsLabel.textColor = self.detailsLabelColor;
  411. detailsLabel.numberOfLines = 0;
  412. detailsLabel.font = self.detailsLabelFont;
  413. detailsLabel.text = self.detailsLabelText;
  414. [self addSubview:detailsLabel];
  415. }
  416. - (void)updateIndicators {
  417. BOOL isActivityIndicator = [indicator isKindOfClass:[UIActivityIndicatorView class]];
  418. BOOL isRoundIndicator = [indicator isKindOfClass:[MBRoundProgressView class]];
  419. if (mode == MBProgressHUDModeIndeterminate) {
  420. if (!isActivityIndicator) {
  421. // Update to indeterminate indicator
  422. [indicator removeFromSuperview];
  423. self.indicator = MB_AUTORELEASE([[UIActivityIndicatorView alloc]
  424. initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]);
  425. [(UIActivityIndicatorView *)indicator startAnimating];
  426. [self addSubview:indicator];
  427. }
  428. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000
  429. [(UIActivityIndicatorView *)indicator setColor:self.activityIndicatorColor];
  430. #endif
  431. }
  432. else if (mode == MBProgressHUDModeDeterminateHorizontalBar) {
  433. // Update to bar determinate indicator
  434. [indicator removeFromSuperview];
  435. self.indicator = MB_AUTORELEASE([[MBBarProgressView alloc] init]);
  436. [self addSubview:indicator];
  437. }
  438. else if (mode == MBProgressHUDModeDeterminate || mode == MBProgressHUDModeAnnularDeterminate) {
  439. if (!isRoundIndicator) {
  440. // Update to determinante indicator
  441. [indicator removeFromSuperview];
  442. self.indicator = MB_AUTORELEASE([[MBRoundProgressView alloc] init]);
  443. [self addSubview:indicator];
  444. }
  445. if (mode == MBProgressHUDModeAnnularDeterminate) {
  446. [(MBRoundProgressView *)indicator setAnnular:YES];
  447. }
  448. }
  449. else if (mode == MBProgressHUDModeCustomView && customView != indicator) {
  450. // Update custom view indicator
  451. [indicator removeFromSuperview];
  452. self.indicator = customView;
  453. [self addSubview:indicator];
  454. } else if (mode == MBProgressHUDModeText) {
  455. [indicator removeFromSuperview];
  456. self.indicator = nil;
  457. }
  458. }
  459. #pragma mark - Layout
  460. - (void)layoutSubviews {
  461. [super layoutSubviews];
  462. // Entirely cover the parent view
  463. UIView *parent = self.superview;
  464. if (parent) {
  465. self.frame = parent.bounds;
  466. }
  467. CGRect bounds = self.bounds;
  468. // Determine the total widt and height needed
  469. CGFloat maxWidth = bounds.size.width - 4 * margin;
  470. CGSize totalSize = CGSizeZero;
  471. CGRect indicatorF = indicator.bounds;
  472. indicatorF.size.width = MIN(indicatorF.size.width, maxWidth);
  473. totalSize.width = MAX(totalSize.width, indicatorF.size.width);
  474. totalSize.height += indicatorF.size.height;
  475. CGSize labelSize = MB_TEXTSIZE(label.text, label.font);
  476. labelSize.width = MIN(labelSize.width, maxWidth);
  477. totalSize.width = MAX(totalSize.width, labelSize.width);
  478. totalSize.height += labelSize.height;
  479. if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
  480. totalSize.height += kPadding;
  481. }
  482. CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
  483. CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
  484. CGSize detailsLabelSize = MB_MULTILINE_TEXTSIZE(detailsLabel.text, detailsLabel.font, maxSize, detailsLabel.lineBreakMode);
  485. totalSize.width = MAX(totalSize.width, detailsLabelSize.width);
  486. totalSize.height += detailsLabelSize.height;
  487. if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {
  488. totalSize.height += kPadding;
  489. }
  490. totalSize.width += 2 * margin;
  491. totalSize.height += 2 * margin;
  492. // Position elements
  493. CGFloat yPos = round(((bounds.size.height - totalSize.height) / 2)) + margin + yOffset;
  494. CGFloat xPos = xOffset;
  495. indicatorF.origin.y = yPos;
  496. indicatorF.origin.x = round((bounds.size.width - indicatorF.size.width) / 2) + xPos;
  497. indicator.frame = indicatorF;
  498. yPos += indicatorF.size.height;
  499. if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
  500. yPos += kPadding;
  501. }
  502. CGRect labelF;
  503. labelF.origin.y = yPos;
  504. labelF.origin.x = round((bounds.size.width - labelSize.width) / 2) + xPos;
  505. labelF.size = labelSize;
  506. label.frame = labelF;
  507. yPos += labelF.size.height;
  508. if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {
  509. yPos += kPadding;
  510. }
  511. CGRect detailsLabelF;
  512. detailsLabelF.origin.y = yPos;
  513. detailsLabelF.origin.x = round((bounds.size.width - detailsLabelSize.width) / 2) + xPos;
  514. detailsLabelF.size = detailsLabelSize;
  515. detailsLabel.frame = detailsLabelF;
  516. // Enforce minsize and quare rules
  517. if (square) {
  518. CGFloat max = MAX(totalSize.width, totalSize.height);
  519. if (max <= bounds.size.width - 2 * margin) {
  520. totalSize.width = max;
  521. }
  522. if (max <= bounds.size.height - 2 * margin) {
  523. totalSize.height = max;
  524. }
  525. }
  526. if (totalSize.width < minSize.width) {
  527. totalSize.width = minSize.width;
  528. }
  529. if (totalSize.height < minSize.height) {
  530. totalSize.height = minSize.height;
  531. }
  532. size = totalSize;
  533. }
  534. #pragma mark BG Drawing
  535. - (void)drawRect:(CGRect)rect {
  536. CGContextRef context = UIGraphicsGetCurrentContext();
  537. UIGraphicsPushContext(context);
  538. if (self.dimBackground) {
  539. //Gradient colours
  540. size_t gradLocationsNum = 2;
  541. CGFloat gradLocations[2] = {0.0f, 1.0f};
  542. CGFloat gradColors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f};
  543. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  544. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
  545. CGColorSpaceRelease(colorSpace);
  546. //Gradient center
  547. CGPoint gradCenter= CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  548. //Gradient radius
  549. float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ;
  550. //Gradient draw
  551. CGContextDrawRadialGradient (context, gradient, gradCenter,
  552. 0, gradCenter, gradRadius,
  553. kCGGradientDrawsAfterEndLocation);
  554. CGGradientRelease(gradient);
  555. }
  556. // Set background rect color
  557. if (self.color) {
  558. CGContextSetFillColorWithColor(context, self.color.CGColor);
  559. } else {
  560. CGContextSetGrayFillColor(context, 0.0f, self.opacity);
  561. }
  562. // Center HUD
  563. CGRect allRect = self.bounds;
  564. // Draw rounded HUD backgroud rect
  565. CGRect boxRect = CGRectMake(round((allRect.size.width - size.width) / 2) + self.xOffset,
  566. round((allRect.size.height - size.height) / 2) + self.yOffset, size.width, size.height);
  567. float radius = self.cornerRadius;
  568. CGContextBeginPath(context);
  569. CGContextMoveToPoint(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));
  570. CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius, 3 * (float)M_PI / 2, 0, 0);
  571. CGContextAddArc(context, CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius, 0, (float)M_PI / 2, 0);
  572. CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius, (float)M_PI / 2, (float)M_PI, 0);
  573. CGContextAddArc(context, CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI, 3 * (float)M_PI / 2, 0);
  574. CGContextClosePath(context);
  575. CGContextFillPath(context);
  576. UIGraphicsPopContext();
  577. }
  578. #pragma mark - KVO
  579. - (void)registerForKVO {
  580. for (NSString *keyPath in [self observableKeypaths]) {
  581. [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
  582. }
  583. }
  584. - (void)unregisterFromKVO {
  585. for (NSString *keyPath in [self observableKeypaths]) {
  586. [self removeObserver:self forKeyPath:keyPath];
  587. }
  588. }
  589. - (NSArray *)observableKeypaths {
  590. return [NSArray arrayWithObjects:@"mode", @"customView", @"labelText", @"labelFont", @"labelColor",
  591. @"detailsLabelText", @"detailsLabelFont", @"detailsLabelColor", @"progress", @"activityIndicatorColor", nil];
  592. }
  593. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  594. if (![NSThread isMainThread]) {
  595. [self performSelectorOnMainThread:@selector(updateUIForKeypath:) withObject:keyPath waitUntilDone:NO];
  596. } else {
  597. [self updateUIForKeypath:keyPath];
  598. }
  599. }
  600. - (void)updateUIForKeypath:(NSString *)keyPath {
  601. if ([keyPath isEqualToString:@"mode"] || [keyPath isEqualToString:@"customView"] ||
  602. [keyPath isEqualToString:@"activityIndicatorColor"]) {
  603. [self updateIndicators];
  604. } else if ([keyPath isEqualToString:@"labelText"]) {
  605. label.text = self.labelText;
  606. } else if ([keyPath isEqualToString:@"labelFont"]) {
  607. label.font = self.labelFont;
  608. } else if ([keyPath isEqualToString:@"labelColor"]) {
  609. label.textColor = self.labelColor;
  610. } else if ([keyPath isEqualToString:@"detailsLabelText"]) {
  611. detailsLabel.text = self.detailsLabelText;
  612. } else if ([keyPath isEqualToString:@"detailsLabelFont"]) {
  613. detailsLabel.font = self.detailsLabelFont;
  614. } else if ([keyPath isEqualToString:@"detailsLabelColor"]) {
  615. detailsLabel.textColor = self.detailsLabelColor;
  616. } else if ([keyPath isEqualToString:@"progress"]) {
  617. if ([indicator respondsToSelector:@selector(setProgress:)]) {
  618. [(id)indicator setValue:@(progress) forKey:@"progress"];
  619. }
  620. return;
  621. }
  622. [self setNeedsLayout];
  623. [self setNeedsDisplay];
  624. }
  625. #pragma mark - Notifications
  626. - (void)registerForNotifications {
  627. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  628. [nc addObserver:self selector:@selector(statusBarOrientationDidChange:)
  629. name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  630. }
  631. - (void)unregisterFromNotifications {
  632. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  633. [nc removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  634. }
  635. - (void)statusBarOrientationDidChange:(NSNotification *)notification {
  636. UIView *superview = self.superview;
  637. if (!superview) {
  638. return;
  639. } else if ([self shouldPerformOrientationTransform]) {
  640. [self setTransformForCurrentOrientation:YES];
  641. } else {
  642. self.frame = self.superview.bounds;
  643. [self setNeedsDisplay];
  644. }
  645. }
  646. - (void)setTransformForCurrentOrientation:(BOOL)animated {
  647. // Stay in sync with the superview
  648. if (self.superview) {
  649. self.bounds = self.superview.bounds;
  650. [self setNeedsDisplay];
  651. }
  652. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  653. CGFloat radians = 0;
  654. if (UIInterfaceOrientationIsLandscape(orientation)) {
  655. if (orientation == UIInterfaceOrientationLandscapeLeft) { radians = -(CGFloat)M_PI_2; }
  656. else { radians = (CGFloat)M_PI_2; }
  657. // Window coordinates differ!
  658. self.bounds = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.width);
  659. } else {
  660. if (orientation == UIInterfaceOrientationPortraitUpsideDown) { radians = (CGFloat)M_PI; }
  661. else { radians = 0; }
  662. }
  663. rotationTransform = CGAffineTransformMakeRotation(radians);
  664. if (animated) {
  665. [UIView beginAnimations:nil context:nil];
  666. [UIView setAnimationDuration:0.3];
  667. }
  668. [self setTransform:rotationTransform];
  669. if (animated) {
  670. [UIView commitAnimations];
  671. }
  672. }
  673. @end
  674. @implementation MBRoundProgressView
  675. #pragma mark - Lifecycle
  676. - (id)init {
  677. return [self initWithFrame:CGRectMake(0.f, 0.f, 37.f, 37.f)];
  678. }
  679. - (id)initWithFrame:(CGRect)frame {
  680. self = [super initWithFrame:frame];
  681. if (self) {
  682. self.backgroundColor = [UIColor clearColor];
  683. self.opaque = NO;
  684. _progress = 0.f;
  685. _annular = NO;
  686. _progressTintColor = [[UIColor alloc] initWithWhite:1.f alpha:1.f];
  687. _backgroundTintColor = [[UIColor alloc] initWithWhite:1.f alpha:.1f];
  688. [self registerForKVO];
  689. }
  690. return self;
  691. }
  692. - (void)dealloc {
  693. [self unregisterFromKVO];
  694. #if !__has_feature(objc_arc)
  695. [_progressTintColor release];
  696. [_backgroundTintColor release];
  697. [super dealloc];
  698. #endif
  699. }
  700. #pragma mark - Drawing
  701. - (void)drawRect:(CGRect)rect {
  702. CGRect allRect = self.bounds;
  703. CGRect circleRect = CGRectInset(allRect, 2.0f, 2.0f);
  704. CGContextRef context = UIGraphicsGetCurrentContext();
  705. if (_annular) {
  706. // Draw background
  707. BOOL isPreiOS7 = NSFoundationVersionNumber < kCFCoreFoundationVersionNumber_iOS_7_0;
  708. CGFloat lineWidth = isPreiOS7 ? 5.f : 2.f;
  709. UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
  710. processBackgroundPath.lineWidth = lineWidth;
  711. processBackgroundPath.lineCapStyle = kCGLineCapButt;
  712. CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  713. CGFloat radius = (self.bounds.size.width - lineWidth)/2;
  714. CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
  715. CGFloat endAngle = (2 * (float)M_PI) + startAngle;
  716. [processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  717. [_backgroundTintColor set];
  718. [processBackgroundPath stroke];
  719. // Draw progress
  720. UIBezierPath *processPath = [UIBezierPath bezierPath];
  721. processPath.lineCapStyle = isPreiOS7 ? kCGLineCapRound : kCGLineCapSquare;
  722. processPath.lineWidth = lineWidth;
  723. endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
  724. [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  725. [_progressTintColor set];
  726. [processPath stroke];
  727. } else {
  728. // Draw background
  729. [_progressTintColor setStroke];
  730. [_backgroundTintColor setFill];
  731. CGContextSetLineWidth(context, 2.0f);
  732. CGContextFillEllipseInRect(context, circleRect);
  733. CGContextStrokeEllipseInRect(context, circleRect);
  734. // Draw progress
  735. CGPoint center = CGPointMake(allRect.size.width / 2, allRect.size.height / 2);
  736. CGFloat radius = (allRect.size.width - 4) / 2;
  737. CGFloat startAngle = - ((float)M_PI / 2); // 90 degrees
  738. CGFloat endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
  739. CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // white
  740. CGContextMoveToPoint(context, center.x, center.y);
  741. CGContextAddArc(context, center.x, center.y, radius, startAngle, endAngle, 0);
  742. CGContextClosePath(context);
  743. CGContextFillPath(context);
  744. }
  745. }
  746. #pragma mark - KVO
  747. - (void)registerForKVO {
  748. for (NSString *keyPath in [self observableKeypaths]) {
  749. [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
  750. }
  751. }
  752. - (void)unregisterFromKVO {
  753. for (NSString *keyPath in [self observableKeypaths]) {
  754. [self removeObserver:self forKeyPath:keyPath];
  755. }
  756. }
  757. - (NSArray *)observableKeypaths {
  758. return [NSArray arrayWithObjects:@"progressTintColor", @"backgroundTintColor", @"progress", @"annular", nil];
  759. }
  760. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  761. [self setNeedsDisplay];
  762. }
  763. @end
  764. @implementation MBBarProgressView
  765. #pragma mark - Lifecycle
  766. - (id)init {
  767. return [self initWithFrame:CGRectMake(.0f, .0f, 120.0f, 20.0f)];
  768. }
  769. - (id)initWithFrame:(CGRect)frame {
  770. self = [super initWithFrame:frame];
  771. if (self) {
  772. _progress = 0.f;
  773. _lineColor = [UIColor whiteColor];
  774. _progressColor = [UIColor whiteColor];
  775. _progressRemainingColor = [UIColor clearColor];
  776. self.backgroundColor = [UIColor clearColor];
  777. self.opaque = NO;
  778. [self registerForKVO];
  779. }
  780. return self;
  781. }
  782. - (void)dealloc {
  783. [self unregisterFromKVO];
  784. #if !__has_feature(objc_arc)
  785. [_lineColor release];
  786. [_progressColor release];
  787. [_progressRemainingColor release];
  788. [super dealloc];
  789. #endif
  790. }
  791. #pragma mark - Drawing
  792. - (void)drawRect:(CGRect)rect {
  793. CGContextRef context = UIGraphicsGetCurrentContext();
  794. CGContextSetLineWidth(context, 2);
  795. CGContextSetStrokeColorWithColor(context,[_lineColor CGColor]);
  796. CGContextSetFillColorWithColor(context, [_progressRemainingColor CGColor]);
  797. // Draw background
  798. float radius = (rect.size.height / 2) - 2;
  799. CGContextMoveToPoint(context, 2, rect.size.height/2);
  800. CGContextAddArcToPoint(context, 2, 2, radius + 2, 2, radius);
  801. CGContextAddLineToPoint(context, rect.size.width - radius - 2, 2);
  802. CGContextAddArcToPoint(context, rect.size.width - 2, 2, rect.size.width - 2, rect.size.height / 2, radius);
  803. CGContextAddArcToPoint(context, rect.size.width - 2, rect.size.height - 2, rect.size.width - radius - 2, rect.size.height - 2, radius);
  804. CGContextAddLineToPoint(context, radius + 2, rect.size.height - 2);
  805. CGContextAddArcToPoint(context, 2, rect.size.height - 2, 2, rect.size.height/2, radius);
  806. CGContextFillPath(context);
  807. // Draw border
  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. CGContextStrokePath(context);
  816. CGContextSetFillColorWithColor(context, [_progressColor CGColor]);
  817. radius = radius - 2;
  818. float amount = self.progress * rect.size.width;
  819. // Progress in the middle area
  820. if (amount >= radius + 4 && amount <= (rect.size.width - radius - 4)) {
  821. CGContextMoveToPoint(context, 4, rect.size.height/2);
  822. CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
  823. CGContextAddLineToPoint(context, amount, 4);
  824. CGContextAddLineToPoint(context, amount, radius + 4);
  825. CGContextMoveToPoint(context, 4, rect.size.height/2);
  826. CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
  827. CGContextAddLineToPoint(context, amount, rect.size.height - 4);
  828. CGContextAddLineToPoint(context, amount, radius + 4);
  829. CGContextFillPath(context);
  830. }
  831. // Progress in the right arc
  832. else if (amount > radius + 4) {
  833. float x = amount - (rect.size.width - radius - 4);
  834. CGContextMoveToPoint(context, 4, rect.size.height/2);
  835. CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
  836. CGContextAddLineToPoint(context, rect.size.width - radius - 4, 4);
  837. float angle = -acos(x/radius);
  838. if (isnan(angle)) angle = 0;
  839. CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, M_PI, angle, 0);
  840. CGContextAddLineToPoint(context, amount, rect.size.height/2);
  841. CGContextMoveToPoint(context, 4, rect.size.height/2);
  842. CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
  843. CGContextAddLineToPoint(context, rect.size.width - radius - 4, rect.size.height - 4);
  844. angle = acos(x/radius);
  845. if (isnan(angle)) angle = 0;
  846. CGContextAddArc(context, rect.size.width - radius - 4, rect.size.height/2, radius, -M_PI, angle, 1);
  847. CGContextAddLineToPoint(context, amount, rect.size.height/2);
  848. CGContextFillPath(context);
  849. }
  850. // Progress is in the left arc
  851. else if (amount < radius + 4 && amount > 0) {
  852. CGContextMoveToPoint(context, 4, rect.size.height/2);
  853. CGContextAddArcToPoint(context, 4, 4, radius + 4, 4, radius);
  854. CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
  855. CGContextMoveToPoint(context, 4, rect.size.height/2);
  856. CGContextAddArcToPoint(context, 4, rect.size.height - 4, radius + 4, rect.size.height - 4, radius);
  857. CGContextAddLineToPoint(context, radius + 4, rect.size.height/2);
  858. CGContextFillPath(context);
  859. }
  860. }
  861. #pragma mark - KVO
  862. - (void)registerForKVO {
  863. for (NSString *keyPath in [self observableKeypaths]) {
  864. [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:NULL];
  865. }
  866. }
  867. - (void)unregisterFromKVO {
  868. for (NSString *keyPath in [self observableKeypaths]) {
  869. [self removeObserver:self forKeyPath:keyPath];
  870. }
  871. }
  872. - (NSArray *)observableKeypaths {
  873. return [NSArray arrayWithObjects:@"lineColor", @"progressRemainingColor", @"progressColor", @"progress", nil];
  874. }
  875. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  876. [self setNeedsDisplay];
  877. }
  878. @end