CustomLoadingView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // CustomLoadingView
  2. // JasonDevelop
  3. //
  4. // Created by Jason Lee on 10/23/14.
  5. // Copyright (c) jasondevelop. All rights reserved.
  6. //
  7. #import "CommonUtil.h"
  8. #import "CustomLoadingView.h"
  9. #import "ImageUtil.h"
  10. @interface CustomLoadingView () {
  11. UIView *_bgView, *_containerView;
  12. UIImageView* _animatedView;
  13. UIActivityIndicatorView *_indicatorView;
  14. UILabel *_lblMessage;
  15. UIButton *_btnCancel;
  16. id targetForExecution;
  17. id objectForExecution;
  18. }
  19. @end
  20. @implementation CustomLoadingView
  21. static CustomLoadingView *__loadingView = nil;
  22. #pragma mark - Singleton
  23. + (CustomLoadingView *)loadingView {
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. CGRect screenRect = [UIScreen mainScreen].bounds;
  27. __loadingView = [[CustomLoadingView alloc] initWithFrame:screenRect];
  28. });
  29. return __loadingView;
  30. }
  31. - (void)showInView:(UIView *)container {
  32. if ([self superview]) {
  33. return;
  34. }
  35. [container addSubview:self];
  36. [self start];
  37. }
  38. - (void)showInView:(UIView *)container whileExecutingBlock:(dispatch_block_t)block {
  39. [self showInView:container whileExecutingBlock:block completionHandler:nil];
  40. }
  41. - (void)showInView:(UIView *)container whileExecutingBlock:(dispatch_block_t)block completionHandler:(JDFacadeCompletionCallBackHandler)completion {
  42. if ([self superview]) {
  43. return;
  44. }
  45. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  46. self.completionBlock = completion;
  47. [container addSubview:self];
  48. [self start];
  49. dispatch_async(queue, ^(void) {
  50. block();
  51. dispatch_async(dispatch_get_main_queue(), ^(void) {
  52. [self cleanUp];
  53. });
  54. });
  55. }
  56. - (void)setOperation:(NSOperation *)operation {
  57. if (!_operation) {
  58. _operation = operation;
  59. }
  60. }
  61. - (void)hide {
  62. if (self) {
  63. [self stop];
  64. [self removeFromSuperview];
  65. }
  66. }
  67. - (id)initWithFrame:(CGRect)frame
  68. {
  69. self = [super initWithFrame:frame];
  70. if (self) {
  71. self.backgroundColor = [UIColor clearColor];
  72. _bgView = [[UIView alloc] initWithFrame:self.frame];
  73. _bgView.backgroundColor = [UIColor darkGrayColor];
  74. _bgView.alpha = 0.35;
  75. [self addSubview:_bgView];
  76. //container
  77. CGFloat multiply = IS_IPHONE_6P ? 1.2f : 1.0f;
  78. _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140 * multiply, 40 * multiply)];
  79. _containerView.backgroundColor = RGBCOLOR(46, 141, 205);
  80. // _containerView.backgroundColor = [UIColor blackColor];
  81. _containerView.layer.cornerRadius = 20.0f * multiply;
  82. CGFloat oriY = (_containerView.height - 24) / 2;
  83. //indicator
  84. CGRect lRect = CGRectMake(oriY, oriY, 24, 24);
  85. _indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:lRect];
  86. _indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
  87. _indicatorView.frame = lRect;
  88. //Label
  89. _lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(42 * multiply, oriY, 60, 24)];
  90. _lblMessage.font = [UIFont systemFontOfSize:14.0f];
  91. _lblMessage.text = @"Loading";
  92. _lblMessage.textColor = [UIColor whiteColor];
  93. _lblMessage.textAlignment = NSTextAlignmentCenter;
  94. //button
  95. _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
  96. _btnCancel.frame = CGRectMake(108 * multiply, oriY, 24, 24);
  97. UIImage *nimage = [ImageUtil resizeImage:[UIImage imageNamed:@"common_btn_loading_close"] width:24 height:24];
  98. UIImage *himage = [ImageUtil resizeImage:[UIImage imageNamed:@"common_btn_loading_close_press"] width:24 height:24];
  99. [_btnCancel setImage:nimage forState:UIControlStateNormal];
  100. [_btnCancel setImage:himage forState:UIControlStateHighlighted];
  101. [_btnCancel addTarget:self action:@selector(btnCancelTouched:) forControlEvents:UIControlEventTouchUpInside];
  102. CGRect center = [CommonUtil getCenterPosition:_containerView fromContainer:self hasNavigationBar:YES hasTabBar:NO];
  103. _containerView.frame = center;
  104. [_containerView addSubview:_indicatorView];
  105. [_containerView addSubview:_lblMessage];
  106. [_containerView addSubview:_btnCancel];
  107. [self addSubview:_containerView];
  108. }
  109. return self;
  110. }
  111. - (void)start {
  112. [_indicatorView startAnimating];
  113. }
  114. - (void)stop {
  115. [_indicatorView stopAnimating];
  116. }
  117. - (void)btnCancelTouched:(id)sender {
  118. if (_operation.isExecuting) {
  119. [_operation cancel];
  120. }
  121. [self cleanUp];
  122. }
  123. - (void)cleanUp {
  124. if (_operation) {
  125. _operation = nil;
  126. }
  127. [self stop];
  128. [self removeFromSuperview];
  129. if (self.completionBlock) {
  130. self.completionBlock();
  131. self.completionBlock = NULL;
  132. }
  133. }
  134. @end