GHRefreshHeader.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // GHRefreshHeader.m
  3. // GameHelper
  4. //
  5. // Created by 青秀斌 on 16/12/29.
  6. // Copyright © 2016年 kylincc. All rights reserved.
  7. //
  8. #import "GHRefreshHeader.h"
  9. #import "UILabel+GHConfig.h"
  10. #import "UIView+GHTheme.h"
  11. #import "XLDotLoading.h"
  12. @interface GHRefreshHeader()
  13. @property (strong, nonatomic) UIImageView *loadingView;
  14. @property (strong, nonatomic) UILabel *stateLabel;
  15. @property (nonatomic, assign) CGFloat anmitationRotate;
  16. @property (nonatomic, assign) BOOL isAnimation;
  17. @end
  18. @implementation GHRefreshHeader
  19. - (void)dealloc{
  20. [self unObserveThemeChange];
  21. }
  22. - (instancetype)init {
  23. self = [super init];
  24. self.textForIdle = @"下拉即可刷新...";
  25. self.textForPulling = @"释放即可刷新...";
  26. self.textForRefreshing = @"正在刷新...";
  27. self.backgroundColor = [UIColor clearColor];
  28. return self;
  29. }
  30. + (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock carRefreshBackgroundType:(CarRefreshBackgroundType)carRefreshBackgroundType
  31. {
  32. GHRefreshHeader *cmp = [super headerWithRefreshingBlock:refreshingBlock];
  33. switch (carRefreshBackgroundType) {
  34. case kCarRefreshBackgroundTypeBlack:{
  35. cmp.textForIdle = @"";
  36. cmp.textForPulling = @"";
  37. cmp.textForRefreshing = @"";
  38. [cmp addSubview:cmp.carRefreshHeadView];
  39. [cmp.carRefreshHeadView startAnimationCarRefreshBackgroundType:kCarRefreshBackgroundTypeBlack];
  40. }break;
  41. case kCarRefreshBackgroundTypeWhite:{
  42. cmp.textForIdle = @"";
  43. cmp.textForPulling = @"";
  44. cmp.textForRefreshing = @"";
  45. [cmp addSubview:cmp.carRefreshHeadView];
  46. [cmp.carRefreshHeadView startAnimationCarRefreshBackgroundType:kCarRefreshBackgroundTypeWhite];
  47. }break;
  48. case kCarRefreshBackgroundTypeDoubleCircle:{
  49. cmp.textForIdle = @"";
  50. cmp.textForPulling = @"";
  51. cmp.textForRefreshing = @"";
  52. cmp.width = kScreenWidth;
  53. cmp.height = cmp.height + 20;
  54. [XLDotLoading showInView:cmp];
  55. }break;
  56. default:{
  57. }break;
  58. }
  59. return cmp;
  60. }
  61. - (CarRefreshHeadView *)carRefreshHeadView{
  62. if(!_carRefreshHeadView){
  63. CarRefreshHeadView *carRefreshHeadView = [[[NSBundle mainBundle] loadNibNamed:@"CarRefreshHeadView" owner:nil options:nil] objectAtIndex:0];
  64. _carRefreshHeadView = carRefreshHeadView;
  65. }
  66. return _carRefreshHeadView;
  67. }
  68. /**********************************************************************/
  69. #pragma mark - OverWrite
  70. /**********************************************************************/
  71. - (void)themeChangeAction{
  72. self.loadingView.image = [UIImage imageNamed:@"下拉刷新"];
  73. self.stateLabel.configText = @"T1";
  74. }
  75. /**********************************************************************/
  76. #pragma mark - Private
  77. /**********************************************************************/
  78. - (void)startAnimation {
  79. if (self.isAnimation) {
  80. return;
  81. }
  82. if (self.state == MJRefreshStateRefreshing) {
  83. self.isAnimation = YES;
  84. __weak __typeof(self) weakSelf = self;
  85. CGAffineTransform angle = CGAffineTransformMakeRotation(fabs(self.anmitationRotate) *(M_PI)/45);
  86. [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  87. weakSelf.loadingView.transform = angle;
  88. } completion:^(BOOL finished) {
  89. weakSelf.anmitationRotate -= 30;
  90. weakSelf.isAnimation = NO;
  91. [weakSelf startAnimation];
  92. }];
  93. }
  94. }
  95. /**********************************************************************/
  96. #pragma mark - OverWrite
  97. /**********************************************************************/
  98. - (void)prepare {
  99. [super prepare];
  100. self.mj_h = 50;
  101. self.automaticallyChangeAlpha = YES;
  102. if (self.loadingView == nil) {
  103. self.loadingView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"下拉刷新"]];
  104. [self addSubview:self.loadingView];
  105. }
  106. if (self.stateLabel == nil) {
  107. self.stateLabel = [[UILabel alloc] init];
  108. self.stateLabel.textAlignment = NSTextAlignmentCenter;
  109. self.stateLabel.font = [UIFont boldSystemFontOfSize:12];
  110. self.stateLabel.backgroundColor = [UIColor clearColor];
  111. [self addSubview:self.stateLabel];
  112. }
  113. [self themeChangeAction];
  114. [self observeThemeChange];
  115. }
  116. - (void)placeSubviews {
  117. [super placeSubviews];
  118. CGRect frame = self.bounds;
  119. frame.origin.x = 20;
  120. frame.size.width -= frame.origin.x;
  121. self.loadingView.center = CGPointMake(self.mj_w * 0.5 - 50, self.mj_h * 0.5);
  122. self.stateLabel.frame = frame;
  123. self.carRefreshHeadView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
  124. }
  125. - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
  126. [super scrollViewContentOffsetDidChange:change];
  127. if (self.scrollView.isDragging && self.state != MJRefreshStateRefreshing) {
  128. self.anmitationRotate = self.scrollView.mj_offsetY;
  129. CGAffineTransform angle = CGAffineTransformMakeRotation(fabs(self.anmitationRotate) *(M_PI)/45);
  130. self.loadingView.transform = angle;
  131. }
  132. }
  133. - (void)setState:(MJRefreshState)state {
  134. MJRefreshCheckState
  135. switch (state) {
  136. case MJRefreshStateIdle:{
  137. self.stateLabel.text = self.textForIdle;
  138. }break;
  139. case MJRefreshStatePulling:{
  140. self.stateLabel.text = self.textForPulling;
  141. }break;
  142. case MJRefreshStateRefreshing:{
  143. self.stateLabel.text = self.textForRefreshing;
  144. [self startAnimation];
  145. }break;
  146. default:break;
  147. }
  148. }
  149. @end