No Description

FKLadderTableView.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // FKLadderTableView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/8/12.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKLadderTableView.h"
  9. #import "FKPullPageTopView.h"
  10. #import "FKPullPageBottomView.h"
  11. @interface FKLadderTableView () <UITableViewDelegate, RefreshControlDelegate>
  12. @property (nonatomic, strong) UITableView *upTableView;
  13. @property (nonatomic, strong) UITableView *downTableView;
  14. @property (nonatomic, strong) UIView *downSegmentView;
  15. @property (nonatomic, weak) id <UITableViewDelegate, UITableViewDataSource, RefreshControlDelegate> tableDelegate;
  16. @end
  17. @implementation FKLadderTableView
  18. - (instancetype)initWithUpTableView:(UITableView *)upTableView
  19. downTableView:(UITableView *)downTableView
  20. downSegmentView:(UIView *)downSegmentView
  21. delegate:(id <UITableViewDelegate, UITableViewDataSource, RefreshControlDelegate>)delegate {
  22. if (self = [super init]) {
  23. self.tableDelegate = delegate;
  24. self.upTableView = upTableView;
  25. self.downTableView = downTableView;
  26. self.downSegmentView = downSegmentView;
  27. self.upTableView.delegate = self;
  28. self.downTableView.delegate = self;
  29. [self addRefresh];
  30. [self addAllSubviews];
  31. }
  32. return self;
  33. }
  34. - (id)forwardingTargetForSelector:(SEL)aSelector{
  35. if ([self.tableDelegate respondsToSelector:aSelector]){
  36. return self.tableDelegate;
  37. }
  38. return self;
  39. }
  40. - (BOOL)respondsToSelector:(SEL)aSelector{
  41. if ([super respondsToSelector:aSelector]){
  42. return YES;
  43. }
  44. return [self.tableDelegate respondsToSelector:aSelector];
  45. }
  46. - (void)addRefresh{
  47. self.upControl = [[FKRefreshControl alloc] initWithScrollView:self.upTableView delegate:self];
  48. [self.upControl registerClassForBottomView:[FKPullPageBottomView class]];
  49. self.upControl.topEnabled = YES;
  50. self.upControl.bottomEnabled = YES;
  51. self.upControl.enableBottomEngage = NO;
  52. self.upControl.bottomRefreshOffsetY = -50;
  53. self.upControl.enableInsetBottom = 46;
  54. self.upControl.enableInsetTop = (IS_IPHONE_X ? 88 : 64);
  55. self.downControl = [[FKRefreshControl alloc] initWithScrollView:self.downTableView delegate:self];
  56. [self.downControl registerClassForTopView:[FKPullPageTopView class]];
  57. self.downControl.autoRefreshBottom = YES;
  58. self.downControl.enableInsetBottom = 0;
  59. self.downControl.enableTopEngage = NO;
  60. self.downControl.topEnabled = YES;
  61. self.downControl.bottomEnabled = YES;
  62. }
  63. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection) direction{
  64. if (refreshControl == self.upControl && direction == RefreshDirectionTop) {
  65. if ([self.tableDelegate respondsToSelector:@selector(refreshControl:didEngageRefreshDirection:)]) {
  66. [self.tableDelegate refreshControl:refreshControl didEngageRefreshDirection:direction];
  67. }
  68. } else if (refreshControl == self.downControl && direction == RefreshDirectionBottom) {
  69. if ([self.tableDelegate respondsToSelector:@selector(refreshControl:didEngageRefreshDirection:)]) {
  70. [self.tableDelegate refreshControl:refreshControl didEngageRefreshDirection:direction];
  71. }
  72. }
  73. }
  74. - (void)addAllSubviews{
  75. [self addSubview:self.bgScrollView];
  76. [self.bgScrollView addSubview:self.upTableView];
  77. [self.bgScrollView addSubview:self.downSegmentView];
  78. [self.bgScrollView addSubview:self.downTableView];
  79. [self.bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.edges.insets(UIEdgeInsetsZero);
  81. }];
  82. [self.upTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.top.left.right.equalTo(self.bgScrollView);
  84. make.width.equalTo(self.bgScrollView);
  85. make.height.equalTo(self.bgScrollView);
  86. }];
  87. [self.downSegmentView mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.equalTo(self.upTableView.mas_bottom);
  89. make.left.right.equalTo(self.bgScrollView);
  90. make.width.equalTo(self.upTableView);
  91. make.height.mas_equalTo(40);
  92. }];
  93. [self.downTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.top.equalTo(self.downSegmentView.mas_bottom);
  95. make.bottom.left.right.equalTo(self.bgScrollView);
  96. make.width.equalTo(self.upTableView);
  97. make.height.equalTo(self.upTableView).offset(- 40 - 20 - 44);
  98. }];
  99. }
  100. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
  101. if (scrollView == self.bgScrollView) {
  102. if (scrollView.contentOffset.y <= 0) {
  103. [self.downControl finishRefreshingDirection:RefreshDirectionTop];
  104. } else if (scrollView.contentOffset.y >= CGRectGetHeight(scrollView.bounds) - 44 - 20) {
  105. [self.upControl finishRefreshingDirection:RefreshDirectionBottom];
  106. }
  107. }
  108. }
  109. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
  110. CGFloat y = scrollView.contentOffset.y;
  111. CGFloat contentSizeY = scrollView.contentSize.height;
  112. if (scrollView == self.upTableView ) {
  113. // 56是详情页bottom的高度
  114. // 46是刷新控件状态变化的距离
  115. CGFloat visibleAreaH = UISCREENHEIGH - 56;
  116. if (y + visibleAreaH - contentSizeY >= 46) {
  117. CGPoint targetPoint = CGPointMake(0, contentSizeY - visibleAreaH);
  118. *targetContentOffset = targetPoint;
  119. [self goToDown];
  120. } else {
  121. [self.upControl finishRefreshingDirection:RefreshDirectionBottom];
  122. [self.upTableView setContentInset:UIEdgeInsetsZero];
  123. }
  124. } else if (scrollView == self.downTableView) {
  125. if (y < 0) {
  126. *targetContentOffset = CGPointZero;
  127. if (y <= - 65) {
  128. [self goToUp];
  129. } else {
  130. [self.downControl finishRefreshingDirection:RefreshDirectionTop];
  131. [self.downTableView setContentInset:UIEdgeInsetsZero];
  132. }
  133. }
  134. }
  135. }
  136. - (void)goToUp{
  137. CGFloat visibleAreaH = UISCREENHEIGH - 56;
  138. CGFloat contentSizeY = self.upTableView.contentSize.height;
  139. CGPoint targetPoint = CGPointMake(0, contentSizeY - visibleAreaH);
  140. self.upTableView.contentOffset = targetPoint;
  141. self.upTableView.scrollsToTop = YES;
  142. self.downTableView.scrollsToTop = NO;
  143. self.bgScrollView.tag = 0;
  144. [self.bgScrollView setContentOffset:CGPointZero animated:YES];
  145. }
  146. - (void)goToDown{
  147. self.upTableView.scrollsToTop = NO;
  148. self.downTableView.scrollsToTop = YES;
  149. self.bgScrollView.tag = 1;
  150. [self.bgScrollView setContentOffset:CGPointMake(0, self.bgScrollView.bounds.size.height - 44 - 20 - (IS_IPHONE_X ? 24 : 0)) animated:YES];
  151. }
  152. - (UIScrollView *)bgScrollView{
  153. if (_bgScrollView == nil) {
  154. _bgScrollView = [[UIScrollView alloc]init];
  155. _bgScrollView.showsVerticalScrollIndicator = NO;
  156. _bgScrollView.showsHorizontalScrollIndicator = NO;
  157. _bgScrollView.backgroundColor = [UIColor whiteColor];
  158. _bgScrollView.scrollEnabled = NO;
  159. _bgScrollView.delegate = self;
  160. }
  161. return _bgScrollView;
  162. }
  163. @end