MJRefreshAutoFooter.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // MJRefreshAutoFooter.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshAutoFooter.h"
  9. @interface MJRefreshAutoFooter()
  10. /** 一个新的拖拽 */
  11. @property (assign, nonatomic, getter=isOneNewPan) BOOL oneNewPan;
  12. @end
  13. @implementation MJRefreshAutoFooter
  14. #pragma mark - 初始化
  15. - (void)willMoveToSuperview:(UIView *)newSuperview
  16. {
  17. [super willMoveToSuperview:newSuperview];
  18. if (newSuperview) { // 新的父控件
  19. if (self.hidden == NO) {
  20. self.scrollView.mj_insetB += self.mj_h;
  21. }
  22. // 设置位置
  23. self.mj_y = _scrollView.mj_contentH;
  24. } else { // 被移除了
  25. if (self.hidden == NO) {
  26. self.scrollView.mj_insetB -= self.mj_h;
  27. }
  28. }
  29. }
  30. #pragma mark - 过期方法
  31. - (void)setAppearencePercentTriggerAutoRefresh:(CGFloat)appearencePercentTriggerAutoRefresh
  32. {
  33. self.triggerAutomaticallyRefreshPercent = appearencePercentTriggerAutoRefresh;
  34. }
  35. - (CGFloat)appearencePercentTriggerAutoRefresh
  36. {
  37. return self.triggerAutomaticallyRefreshPercent;
  38. }
  39. #pragma mark - 实现父类的方法
  40. - (void)prepare
  41. {
  42. [super prepare];
  43. // 默认底部控件100%出现时才会自动刷新
  44. self.triggerAutomaticallyRefreshPercent = 1.0;
  45. // 设置为默认状态
  46. self.automaticallyRefresh = YES;
  47. // 默认是当offset达到条件就发送请求(可连续)
  48. self.onlyRefreshPerDrag = NO;
  49. }
  50. - (void)scrollViewContentSizeDidChange:(NSDictionary *)change
  51. {
  52. [super scrollViewContentSizeDidChange:change];
  53. // 设置位置
  54. self.mj_y = self.scrollView.mj_contentH;
  55. }
  56. - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
  57. {
  58. [super scrollViewContentOffsetDidChange:change];
  59. if (self.state != MJRefreshStateIdle || !self.automaticallyRefresh || self.mj_y == 0) return;
  60. if (_scrollView.mj_insetT + _scrollView.mj_contentH > _scrollView.mj_h) { // 内容超过一个屏幕
  61. // 这里的_scrollView.mj_contentH替换掉self.mj_y更为合理
  62. if (_scrollView.mj_offsetY >= _scrollView.mj_contentH - _scrollView.mj_h + self.mj_h * self.triggerAutomaticallyRefreshPercent + _scrollView.mj_insetB - self.mj_h) {
  63. // 防止手松开时连续调用
  64. CGPoint old = [change[@"old"] CGPointValue];
  65. CGPoint new = [change[@"new"] CGPointValue];
  66. if (new.y <= old.y) return;
  67. // 当底部刷新控件完全出现时,才刷新
  68. [self beginRefreshing];
  69. }
  70. }
  71. }
  72. - (void)scrollViewPanStateDidChange:(NSDictionary *)change
  73. {
  74. [super scrollViewPanStateDidChange:change];
  75. if (self.state != MJRefreshStateIdle) return;
  76. UIGestureRecognizerState panState = _scrollView.panGestureRecognizer.state;
  77. if (panState == UIGestureRecognizerStateEnded) {// 手松开
  78. if (_scrollView.mj_insetT + _scrollView.mj_contentH <= _scrollView.mj_h) { // 不够一个屏幕
  79. if (_scrollView.mj_offsetY >= - _scrollView.mj_insetT) { // 向上拽
  80. [self beginRefreshing];
  81. }
  82. } else { // 超出一个屏幕
  83. if (_scrollView.mj_offsetY >= _scrollView.mj_contentH + _scrollView.mj_insetB - _scrollView.mj_h) {
  84. [self beginRefreshing];
  85. }
  86. }
  87. } else if (panState == UIGestureRecognizerStateBegan) {
  88. self.oneNewPan = YES;
  89. }
  90. }
  91. - (void)beginRefreshing
  92. {
  93. if (!self.isOneNewPan && self.isOnlyRefreshPerDrag) return;
  94. [super beginRefreshing];
  95. self.oneNewPan = NO;
  96. }
  97. - (void)setState:(MJRefreshState)state
  98. {
  99. MJRefreshCheckState
  100. if (state == MJRefreshStateRefreshing) {
  101. [self executeRefreshingCallback];
  102. } else if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
  103. if (MJRefreshStateRefreshing == oldState) {
  104. if (self.endRefreshingCompletionBlock) {
  105. self.endRefreshingCompletionBlock();
  106. }
  107. }
  108. }
  109. }
  110. - (void)setHidden:(BOOL)hidden
  111. {
  112. BOOL lastHidden = self.isHidden;
  113. [super setHidden:hidden];
  114. if (!lastHidden && hidden) {
  115. self.state = MJRefreshStateIdle;
  116. self.scrollView.mj_insetB -= self.mj_h;
  117. } else if (lastHidden && !hidden) {
  118. self.scrollView.mj_insetB += self.mj_h;
  119. // 设置位置
  120. self.mj_y = _scrollView.mj_contentH;
  121. }
  122. }
  123. @end