UITableView 嵌套tableView吸顶 手势冲突问题解决方案以及优化(一 解决)
自定义的 可以识别多个手势的 ContainerTableView 最外层的tableView
@interface LXContainerTableView : UITableView
@end
/**
同时识别多个手势
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]) {
if (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && self.contentOffset.x == 0) {
return NO;
}
}
return YES;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (self.tableHeaderView && CGRectContainsPoint(self.tableHeaderView.frame, point)) {
return NO;
}
return [super pointInside:point withEvent:event];
}
mainPageVC
初始化 mainTableView 时,默认可滑动
-(LXContainerTableView *)mainTableView{
if (!_mainTableView) {
// 是否可mainTableView滑动
_canScroll = YES;
_mainTableView = [[LXContainerTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - kiphonexBottom) style:UITableViewStylePlain];
_mainTableView.delegate = self;
_mainTableView.dataSource = self;
_mainTableView.backgroundColor = [UIColor getBackgroundColor];
_mainTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
_mainTableView.showsVerticalScrollIndicator = NO;
_mainTableView.tableHeaderView = self.headerView;
if (@available(iOS 11.0, *)) {
_mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
_mainTableView.tableFooterView = self.footerView;
}
return _mainTableView;
}
matableViewVC 中 scrollViewDisScroll 代理方法中 告知子视图:父视图已经吸顶,子视图可以滑动了
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.mainTableView) {
// 调整 导航条的透明度
CGFloat alpha = self.mainTableView.contentOffset.y/100.f;
[self.navBarView updateAlpha:alpha];
//
CGFloat maxOffset = self.headerView.height - kNavigationBarH;
if (self.mainTableView.contentOffset.y > maxOffset) {
scrollView.contentOffset = CGPointMake(0, maxOffset);
_canScroll = NO;
// header 已经滑动到顶部,让子视图可以滚动
[self headerScrollToTop];
}else{
if (!_canScroll) {
self.mainTableView.contentOffset = CGPointMake(0, maxOffset);
}
}
}
}
// --- header 已经滑动到顶部,让子视图可以滚动
-(void)headerScrollToTop{
[self.baseInfoVC sectionHeaderToTopChildVCScroll:YES];
[self.courseVC sectionHeaderToTopChildVCScroll:YES];
}
子VC的baseVC内容
子VC的baseVC 的 .h文件
```
@interface LXTeacherTabBaseVC : LXBaseViewController<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
/** 通知父scrollView 取消吸顶 */
@property (nonatomic,copy) void (^ leaveTopAction)(BOOL canScroll);
/** 嵌套的scrollView的子View */
@property (nonatomic, strong) UIScrollView *baseScrollView;
/** 子scrollView视图 是否可滑动 */
@property (nonatomic, assign) BOOL canScroll;
/** mainTableView 的 header 已经滑动到顶部,让子视图可以滚动 */
-(void)sectionHeaderToTopChildVCScroll:(BOOL)canScroll;
/** 重新加载数据【网路数据重新加载】 */
-(void)reloadData;
@end
```
子VC的baseVC 的 .m文件
```
// 设置基础scrollView
-(void)setBaseScrollView:(UIScrollView *)baseScrollView{
_baseScrollView = baseScrollView;
_baseScrollView.scrollEnabled = YES;
}
// 控制子视图滑动功能的开启
-(void)sectionHeaderToTopChildVCScroll:(BOOL)canScroll{
if (canScroll) {
self.baseScrollView.scrollEnabled = YES;
self.baseScrollView.showsVerticalScrollIndicator = YES;
_canScroll = YES;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.baseScrollView) {
CGFloat offsetY = scrollView.contentOffset.y;
if (!_canScroll) {
[scrollView setContentOffset:CGPointZero];
}
if (offsetY <= 0) {
_canScroll = NO;
if (self.leaveTopAction) {
self.leaveTopAction(YES);
}
}
self.baseScrollView.showsVerticalScrollIndicator = _canScroll;
}
}
```
mainTableVC 中 捕获子视图的回调方法
```
// 分别设置子视图的回调
_baseInfoVC.leaveTopAction = ^(BOOL canScroll) {
[wself childVCLeaveTopMainTableEnableScroll:canScroll];
};
// 统一处理回调
#pragma mark ------- subVC callback
-(void)childVCLeaveTopMainTableEnableScroll:(BOOL)canScroll{
self.canScroll = YES;
}
```
子类
baseVC的 初始化时候设置baseScrollView
```
self.baseScrollView = self.tableView;
```
or
```
self.baseScrollView = _scrollView;
```
##【上下滑 左右滑 冲突解决】
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (scrollView == self.swipScrollView) {
self.mainTableView.scrollEnabled = NO;
}
if (scrollView == self.mainTableView) {
self.swipScrollView.scrollEnabled = NO;
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (scrollView == self.swipScrollView) {
self.mainTableView.scrollEnabled = YES;
}
if (scrollView == self.mainTableView) {
self.swipScrollView.scrollEnabled = YES;
}
}