MJRefreshFooterView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // MJRefreshFooterView.m
  3. // MJRefresh
  4. //
  5. // Created by mj on 13-2-26.
  6. // Copyright (c) 2013年 itcast. All rights reserved.
  7. // 上拉加载更多
  8. #import "MJRefreshFooterView.h"
  9. #import "MJRefreshConst.h"
  10. @interface MJRefreshFooterView()
  11. //{
  12. // BOOL _withoutIdle;
  13. //}
  14. {
  15. int _lastRefreshCount;
  16. }
  17. @end
  18. @implementation MJRefreshFooterView
  19. + (instancetype)footer
  20. {
  21. return [[MJRefreshFooterView alloc] init];
  22. }
  23. #pragma mark - 初始化
  24. - (instancetype)initWithFrame:(CGRect)frame {
  25. if (self = [super initWithFrame: frame]) {
  26. // 移除刷新时间
  27. [_lastUpdateTimeLabel removeFromSuperview];
  28. _lastUpdateTimeLabel = nil;
  29. }
  30. return self;
  31. }
  32. -(void)dealloc{
  33. NSLog(@"MJRefreshFooterView.dealloc");
  34. }
  35. - (void)setFrame:(CGRect)frame
  36. {
  37. [super setFrame:frame];
  38. CGFloat h = frame.size.height;
  39. if (_statusLabel.center.y != h * 0.5) {
  40. CGFloat w = frame.size.width;
  41. _statusLabel.center = CGPointMake(w * 0.5, h * 0.5);
  42. }
  43. }
  44. #pragma mark - UIScrollView相关
  45. #pragma mark 重写设置ScrollView
  46. - (void)setScrollView:(UIScrollView *)scrollView
  47. {
  48. // 1.移除以前的监听器
  49. [_scrollView removeObserver:self forKeyPath:MJRefreshContentSize context:nil];
  50. // 2.监听contentSize
  51. [scrollView addObserver:self forKeyPath:MJRefreshContentSize options:NSKeyValueObservingOptionNew context:nil];
  52. // 3.父类的方法
  53. [super setScrollView:scrollView];
  54. // 4.重新调整frame
  55. [self adjustFrame];
  56. }
  57. #pragma mark 监听UIScrollView的属性
  58. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  59. {
  60. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  61. if (!self.userInteractionEnabled || self.alpha <= 0.01 || self.hidden) return;
  62. if ([MJRefreshContentSize isEqualToString:keyPath]) {
  63. [self adjustFrame];
  64. }
  65. }
  66. #pragma mark 重写调整frame
  67. - (void)adjustFrame
  68. {
  69. // 内容的高度
  70. CGFloat contentHeight = _scrollView.contentSize.height;
  71. // 表格的高度
  72. CGFloat scrollHeight = _scrollView.frame.size.height - _scrollViewInitInset.top - _scrollViewInitInset.bottom;
  73. CGFloat y = MAX(contentHeight, scrollHeight);
  74. // 设置边框
  75. self.frame = CGRectMake(0, y, _scrollView.frame.size.width, MJRefreshViewHeight);
  76. }
  77. #pragma mark - 状态相关
  78. #pragma mark 设置状态
  79. - (void)setState:(MJRefreshState)state
  80. {
  81. if (_state == state) return;
  82. MJRefreshState oldState = _state;
  83. [super setState:state];
  84. switch (state)
  85. {
  86. case MJRefreshStatePulling:
  87. {
  88. _statusLabel.text = MJRefreshFooterReleaseToRefresh;
  89. [UIView animateWithDuration:MJRefreshAnimationDuration animations:^{
  90. _arrowImage.transform = CGAffineTransformIdentity;
  91. UIEdgeInsets inset = _scrollView.contentInset;
  92. inset.bottom = _scrollViewInitInset.bottom;
  93. _scrollView.contentInset = inset;
  94. }];
  95. break;
  96. }
  97. case MJRefreshStateNormal:
  98. {
  99. _statusLabel.text = MJRefreshFooterPullToRefresh;
  100. // 刚刷新完毕
  101. CGFloat animDuration = MJRefreshAnimationDuration;
  102. CGFloat deltaH = [self contentBreakView];
  103. CGPoint tempOffset;
  104. int currentCount = [self totalDataCountInScrollView];
  105. if (MJRefreshStateRefreshing == oldState && deltaH > 0 && currentCount != _lastRefreshCount) {
  106. tempOffset = _scrollView.contentOffset;
  107. animDuration = 0;
  108. }
  109. [UIView animateWithDuration:animDuration animations:^{
  110. _arrowImage.transform = CGAffineTransformMakeRotation(M_PI);
  111. UIEdgeInsets inset = _scrollView.contentInset;
  112. inset.bottom = _scrollViewInitInset.bottom;
  113. _scrollView.contentInset = inset;
  114. }];
  115. if (animDuration == 0) {
  116. _scrollView.contentOffset = tempOffset;
  117. }
  118. break;
  119. }
  120. case MJRefreshStateRefreshing:
  121. {
  122. // 记录刷新前的数量
  123. _lastRefreshCount = [self totalDataCountInScrollView];
  124. _statusLabel.text = MJRefreshFooterRefreshing;
  125. _arrowImage.transform = CGAffineTransformMakeRotation(M_PI);
  126. [UIView animateWithDuration:MJRefreshAnimationDuration animations:^{
  127. UIEdgeInsets inset = _scrollView.contentInset;
  128. CGFloat bottom = MJRefreshViewHeight + _scrollViewInitInset.bottom;
  129. CGFloat deltaH = [self contentBreakView];
  130. if (deltaH < 0) { // 如果内容高度小于view的高度
  131. bottom -= deltaH;
  132. }
  133. inset.bottom = bottom;
  134. _scrollView.contentInset = inset;
  135. }];
  136. break;
  137. }
  138. default:
  139. break;
  140. }
  141. }
  142. //- (void)endRefreshingWithoutIdle
  143. //{
  144. // _withoutIdle = YES;
  145. // [self endRefreshing];
  146. // _withoutIdle = NO;
  147. //}
  148. #pragma mark 获得scrollView的内容 超出 view 的高度
  149. - (CGFloat)contentBreakView
  150. {
  151. CGFloat h = _scrollView.frame.size.height - _scrollViewInitInset.bottom - _scrollViewInitInset.top;
  152. return _scrollView.contentSize.height - h;
  153. }
  154. #pragma mark - 在父类中用得上
  155. // 合理的Y值(刚好看到上拉刷新控件时的contentOffset.y,取相反数)
  156. - (CGFloat)validY
  157. {
  158. CGFloat deltaH = [self contentBreakView];
  159. if (deltaH > 0) {
  160. return deltaH -_scrollViewInitInset.top;
  161. } else {
  162. return -_scrollViewInitInset.top;
  163. }
  164. }
  165. // view的类型
  166. - (int)viewType
  167. {
  168. return MJRefreshViewTypeFooter;
  169. }
  170. - (void)free
  171. {
  172. [super free];
  173. [_scrollView removeObserver:self forKeyPath:MJRefreshContentSize];
  174. }
  175. @end