// // FKLadderTableView.m // FirstLink // // Created by jack on 16/8/12. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKLadderTableView.h" #import "FKPullPageTopView.h" #import "FKPullPageBottomView.h" @interface FKLadderTableView () @property (nonatomic, strong) UITableView *upTableView; @property (nonatomic, strong) UITableView *downTableView; @property (nonatomic, strong) UIView *downSegmentView; @property (nonatomic, weak) id tableDelegate; @end @implementation FKLadderTableView - (instancetype)initWithUpTableView:(UITableView *)upTableView downTableView:(UITableView *)downTableView downSegmentView:(UIView *)downSegmentView delegate:(id )delegate { if (self = [super init]) { self.tableDelegate = delegate; self.upTableView = upTableView; self.downTableView = downTableView; self.downSegmentView = downSegmentView; self.upTableView.delegate = self; self.downTableView.delegate = self; [self addRefresh]; [self addAllSubviews]; } return self; } - (id)forwardingTargetForSelector:(SEL)aSelector{ if ([self.tableDelegate respondsToSelector:aSelector]){ return self.tableDelegate; } return self; } - (BOOL)respondsToSelector:(SEL)aSelector{ if ([super respondsToSelector:aSelector]){ return YES; } return [self.tableDelegate respondsToSelector:aSelector]; } - (void)addRefresh{ self.upControl = [[FKRefreshControl alloc] initWithScrollView:self.upTableView delegate:self]; [self.upControl registerClassForBottomView:[FKPullPageBottomView class]]; self.upControl.topEnabled = YES; self.upControl.bottomEnabled = YES; self.upControl.enableBottomEngage = NO; self.upControl.bottomRefreshOffsetY = -50; self.upControl.enableInsetBottom = 46; self.upControl.enableInsetTop = (IS_IPHONE_X ? 88 : 64); self.downControl = [[FKRefreshControl alloc] initWithScrollView:self.downTableView delegate:self]; [self.downControl registerClassForTopView:[FKPullPageTopView class]]; self.downControl.autoRefreshBottom = YES; self.downControl.enableInsetBottom = 0; self.downControl.enableTopEngage = NO; self.downControl.topEnabled = YES; self.downControl.bottomEnabled = YES; } - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection) direction{ if (refreshControl == self.upControl && direction == RefreshDirectionTop) { if ([self.tableDelegate respondsToSelector:@selector(refreshControl:didEngageRefreshDirection:)]) { [self.tableDelegate refreshControl:refreshControl didEngageRefreshDirection:direction]; } } else if (refreshControl == self.downControl && direction == RefreshDirectionBottom) { if ([self.tableDelegate respondsToSelector:@selector(refreshControl:didEngageRefreshDirection:)]) { [self.tableDelegate refreshControl:refreshControl didEngageRefreshDirection:direction]; } } } - (void)addAllSubviews{ [self addSubview:self.bgScrollView]; [self.bgScrollView addSubview:self.upTableView]; [self.bgScrollView addSubview:self.downSegmentView]; [self.bgScrollView addSubview:self.downTableView]; [self.bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [self.upTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.bgScrollView); make.width.equalTo(self.bgScrollView); make.height.equalTo(self.bgScrollView); }]; [self.downSegmentView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.upTableView.mas_bottom); make.left.right.equalTo(self.bgScrollView); make.width.equalTo(self.upTableView); make.height.mas_equalTo(40); }]; [self.downTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.downSegmentView.mas_bottom); make.bottom.left.right.equalTo(self.bgScrollView); make.width.equalTo(self.upTableView); make.height.equalTo(self.upTableView).offset(- 40 - 20 - 44); }]; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ if (scrollView == self.bgScrollView) { if (scrollView.contentOffset.y <= 0) { [self.downControl finishRefreshingDirection:RefreshDirectionTop]; } else if (scrollView.contentOffset.y >= CGRectGetHeight(scrollView.bounds) - 44 - 20) { [self.upControl finishRefreshingDirection:RefreshDirectionBottom]; } } } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ CGFloat y = scrollView.contentOffset.y; CGFloat contentSizeY = scrollView.contentSize.height; if (scrollView == self.upTableView ) { // 56是详情页bottom的高度 // 46是刷新控件状态变化的距离 CGFloat visibleAreaH = UISCREENHEIGH - 56; if (y + visibleAreaH - contentSizeY >= 46) { CGPoint targetPoint = CGPointMake(0, contentSizeY - visibleAreaH); *targetContentOffset = targetPoint; [self goToDown]; } else { [self.upControl finishRefreshingDirection:RefreshDirectionBottom]; [self.upTableView setContentInset:UIEdgeInsetsZero]; } } else if (scrollView == self.downTableView) { if (y < 0) { *targetContentOffset = CGPointZero; if (y <= - 65) { [self goToUp]; } else { [self.downControl finishRefreshingDirection:RefreshDirectionTop]; [self.downTableView setContentInset:UIEdgeInsetsZero]; } } } } - (void)goToUp{ CGFloat visibleAreaH = UISCREENHEIGH - 56; CGFloat contentSizeY = self.upTableView.contentSize.height; CGPoint targetPoint = CGPointMake(0, contentSizeY - visibleAreaH); self.upTableView.contentOffset = targetPoint; self.upTableView.scrollsToTop = YES; self.downTableView.scrollsToTop = NO; self.bgScrollView.tag = 0; [self.bgScrollView setContentOffset:CGPointZero animated:YES]; } - (void)goToDown{ self.upTableView.scrollsToTop = NO; self.downTableView.scrollsToTop = YES; self.bgScrollView.tag = 1; [self.bgScrollView setContentOffset:CGPointMake(0, self.bgScrollView.bounds.size.height - 44 - 20 - (IS_IPHONE_X ? 24 : 0)) animated:YES]; } - (UIScrollView *)bgScrollView{ if (_bgScrollView == nil) { _bgScrollView = [[UIScrollView alloc]init]; _bgScrollView.showsVerticalScrollIndicator = NO; _bgScrollView.showsHorizontalScrollIndicator = NO; _bgScrollView.backgroundColor = [UIColor whiteColor]; _bgScrollView.scrollEnabled = NO; _bgScrollView.delegate = self; } return _bgScrollView; } @end