FanweCustomHud.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // FanweCustomHud.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 2017/3/9.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "FanweCustomHud.h"
  9. @interface FanweCustomHud()
  10. {
  11. UIView * _loadingBackground; // 加载中的背景
  12. UIImageView * _loadingImageView; // 加载中的背景图
  13. UITextView * _textView;
  14. }
  15. @end
  16. @implementation FanweCustomHud
  17. static id _instance;
  18. + (instancetype)allocWithZone:(struct _NSZone *)zone
  19. {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. _instance = [super allocWithZone:zone];
  23. [_instance initMyData];
  24. });
  25. return _instance;
  26. }
  27. + (instancetype)sharedInstance
  28. {
  29. static dispatch_once_t onceToken;
  30. dispatch_once(&onceToken, ^{
  31. _instance = [[self alloc] init];
  32. [_instance initMyData];
  33. });
  34. return _instance;
  35. }
  36. - (id)copyWithZone:(NSZone *)zone
  37. {
  38. return _instance;
  39. }
  40. - (id)mutableCopyWithZone:(NSZone *)zone
  41. {
  42. return _instance;
  43. }
  44. - (void)initMyData
  45. {
  46. if (_loadingBackground == nil)
  47. {
  48. _loadingBackground = [[UIView alloc] init];
  49. _loadingBackground.hidden = YES;
  50. _loadingBackground.backgroundColor = kClearColor;
  51. _loadingBackground.alpha = 0.5;
  52. _textView = [[UITextView alloc]init];
  53. _textView.textAlignment = NSTextAlignmentCenter;
  54. _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  55. _textView.textColor = [UIColor blackColor];
  56. _textView.hidden = YES;
  57. }
  58. if (_loadingImageView == nil)
  59. {
  60. float width = 50;
  61. float height = 50;
  62. NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"loading_image0.png"],
  63. [UIImage imageNamed:@"loading_image1.png"],
  64. [UIImage imageNamed:@"loading_image2.png"],
  65. [UIImage imageNamed:@"loading_image3.png"],
  66. [UIImage imageNamed:@"loading_image4.png"],
  67. [UIImage imageNamed:@"loading_image5.png"],
  68. [UIImage imageNamed:@"loading_image6.png"],
  69. [UIImage imageNamed:@"loading_image7.png"],
  70. [UIImage imageNamed:@"loading_image8.png"],
  71. [UIImage imageNamed:@"loading_image9.png"],
  72. [UIImage imageNamed:@"loading_image10.png"],
  73. [UIImage imageNamed:@"loading_image11.png"],
  74. [UIImage imageNamed:@"loading_image12.png"],
  75. [UIImage imageNamed:@"loading_image13.png"],
  76. [UIImage imageNamed:@"loading_image14.png"],
  77. nil];
  78. _loadingImageView = [[UIImageView alloc] init];
  79. _loadingImageView.bounds = CGRectMake(0, 0, width, height);
  80. _loadingImageView.animationImages = array;
  81. _loadingImageView.animationDuration = 1;
  82. _loadingImageView.hidden = YES;
  83. }
  84. }
  85. - (void)startLoadingInView:(UIView*)view tipMsg:(NSString *)tipMsg
  86. {
  87. CGRect rect = view.frame;
  88. if (_loadingBackground)
  89. {
  90. _loadingBackground.frame = CGRectMake(0, 0, CGRectGetWidth(rect), CGRectGetHeight(rect));
  91. [view addSubview:_loadingBackground];
  92. _loadingBackground.hidden = NO;
  93. _textView.bounds = CGRectMake(0, 0, CGRectGetWidth(rect), 30);
  94. _textView.center = CGPointMake(CGRectGetWidth(rect) / 2, CGRectGetHeight(rect) / 2 - 30);
  95. [_loadingBackground addSubview:_textView];
  96. if (![BGUtils isBlankString:tipMsg])
  97. {
  98. _textView.text = tipMsg;
  99. _textView.hidden = NO;
  100. }
  101. else
  102. {
  103. _textView.hidden = YES;
  104. }
  105. }
  106. if (_loadingImageView)
  107. {
  108. _loadingImageView.center = CGPointMake(CGRectGetWidth(rect) / 2, CGRectGetHeight(rect) / 2);
  109. [view addSubview:_loadingImageView];
  110. _loadingImageView.hidden = NO;
  111. [_loadingImageView startAnimating];
  112. }
  113. }
  114. - (void)stopLoading
  115. {
  116. if (_loadingBackground)
  117. {
  118. _loadingBackground.hidden = YES;
  119. }
  120. if (_loadingImageView)
  121. {
  122. _loadingImageView.hidden = YES;
  123. [_loadingImageView stopAnimating];
  124. }
  125. }
  126. @end