MJRefreshAutoFooter.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. @end
  11. @implementation MJRefreshAutoFooter
  12. #pragma mark - 初始化
  13. - (void)willMoveToSuperview:(UIView *)newSuperview
  14. {
  15. [super willMoveToSuperview:newSuperview];
  16. if (newSuperview) { // 新的父控件
  17. if (self.hidden == NO) {
  18. self.scrollView.mj_insetB += self.mj_h;
  19. }
  20. // 设置位置
  21. self.mj_y = _scrollView.mj_contentH;
  22. } else { // 被移除了
  23. if (self.hidden == NO) {
  24. self.scrollView.mj_insetB -= self.mj_h;
  25. }
  26. }
  27. }
  28. #pragma mark - 过期方法
  29. - (void)setAppearencePercentTriggerAutoRefresh:(CGFloat)appearencePercentTriggerAutoRefresh
  30. {
  31. self.triggerAutomaticallyRefreshPercent = appearencePercentTriggerAutoRefresh;
  32. }
  33. - (CGFloat)appearencePercentTriggerAutoRefresh
  34. {
  35. return self.triggerAutomaticallyRefreshPercent;
  36. }
  37. #pragma mark - 实现父类的方法
  38. - (void)prepare
  39. {
  40. [super prepare];
  41. // 默认底部控件100%出现时才会自动刷新
  42. self.triggerAutomaticallyRefreshPercent = 1.0;
  43. // 设置为默认状态
  44. self.automaticallyRefresh = YES;
  45. }
  46. - (void)scrollViewContentSizeDidChange:(NSDictionary *)change
  47. {
  48. [super scrollViewContentSizeDidChange:change];
  49. // 设置位置
  50. self.mj_y = self.scrollView.mj_contentH;
  51. }
  52. - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
  53. {
  54. [super scrollViewContentOffsetDidChange:change];
  55. if (self.state != MJRefreshStateIdle || !self.automaticallyRefresh || self.mj_y == 0) return;
  56. if (_scrollView.mj_insetT + _scrollView.mj_contentH > _scrollView.mj_h) { // 内容超过一个屏幕
  57. // 这里的_scrollView.mj_contentH替换掉self.mj_y更为合理
  58. if (_scrollView.mj_offsetY >= _scrollView.mj_contentH - _scrollView.mj_h + self.mj_h * self.triggerAutomaticallyRefreshPercent + _scrollView.mj_insetB - self.mj_h) {
  59. // 防止手松开时连续调用
  60. CGPoint old = [change[@"old"] CGPointValue];
  61. CGPoint new = [change[@"new"] CGPointValue];
  62. if (new.y <= old.y) return;
  63. // 当底部刷新控件完全出现时,才刷新
  64. [self beginRefreshing];
  65. }
  66. }
  67. }
  68. - (void)scrollViewPanStateDidChange:(NSDictionary *)change
  69. {
  70. [super scrollViewPanStateDidChange:change];
  71. if (self.state != MJRefreshStateIdle) return;
  72. if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateEnded) {// 手松开
  73. if (_scrollView.mj_insetT + _scrollView.mj_contentH <= _scrollView.mj_h) { // 不够一个屏幕
  74. if (_scrollView.mj_offsetY >= - _scrollView.mj_insetT) { // 向上拽
  75. [self beginRefreshing];
  76. }
  77. } else { // 超出一个屏幕
  78. if (_scrollView.mj_offsetY >= _scrollView.mj_contentH + _scrollView.mj_insetB - _scrollView.mj_h) {
  79. [self beginRefreshing];
  80. }
  81. }
  82. }
  83. }
  84. - (void)setState:(MJRefreshState)state
  85. {
  86. MJRefreshCheckState
  87. if (state == MJRefreshStateRefreshing) {
  88. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  89. [self executeRefreshingCallback];
  90. });
  91. }
  92. }
  93. - (void)setHidden:(BOOL)hidden
  94. {
  95. BOOL lastHidden = self.isHidden;
  96. [super setHidden:hidden];
  97. if (!lastHidden && hidden) {
  98. self.state = MJRefreshStateIdle;
  99. self.scrollView.mj_insetB -= self.mj_h;
  100. } else if (lastHidden && !hidden) {
  101. self.scrollView.mj_insetB += self.mj_h;
  102. // 设置位置
  103. self.mj_y = _scrollView.mj_contentH;
  104. }
  105. }
  106. @end