CustomLoadingView.m 4.3 KB

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