最近比較流行的布局就是頁(yè)面有個(gè)tab頁(yè)下面是一個(gè)或者幾個(gè)tableView可以左右滑動(dòng)吁伺,tab頁(yè)滑到頂部置頂懸浮逞带。
思路:一個(gè)scrollView鑲嵌一個(gè)scrollView中利用代理監(jiān)聽(tīng)他們的偏移量欺矫,設(shè)置一個(gè)臨界值,沒(méi)過(guò)這個(gè)值主scrollView可以滑動(dòng)展氓,子scrollView不可以滑動(dòng)穆趴,當(dāng)超過(guò)這個(gè)值,主scollView不可以滑動(dòng)遇汞,子scollView可以滑動(dòng)未妹。
首先創(chuàng)建兩個(gè)scrollView簿废,MainScrollView和ContentScrollView都繼承于UIScrollView。
MainScrollView.m文件:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
支持多手勢(shì)
ContentScollView里面可以添加一個(gè)或者多個(gè)UITableView
1络它、在控制器中設(shè)置topViewHeight(topView的高度)和floatHeight(懸浮的高度)族檬,設(shè)置主子scrollView是否可以滑動(dòng)
self.topViewHeight = 300;
self.floatHeight = 200;
self.canMove = YES;
self.contentCanMove = NO;
2、懶加載MainScrollView和ContentScollView
- (MainScrollView *)mainScrollView
{
if (!_mainScrollView)
{
CGFloat height = kScreenHeight - 44 - [UIDevice statusBarHeight] - [UIDevice tabBarHeight];
_mainScrollView = [[MainScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
_mainScrollView.delegate = self;
_mainScrollView.contentSize = CGSizeMake(0, height + self.topViewHeight);
if (@available(iOS 11.0, *))
{
_mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
return _mainScrollView;
}
- (ContentScrollView *)contentScrollView
{
if (!_contentScrollView)
{
CGFloat height = kScreenHeight - 44 - [UIDevice statusBarHeight] - [UIDevice tabBarHeight];
_contentScrollView = [[ContentScrollView alloc] initWithFrame: CGRectMake(0, self.topViewHeight, kScreenWidth, height - self.floatHeight)];
_contentScrollView.tableView.delegate = self;
_contentScrollView.tableView.dataSource = self;
}
return _contentScrollView;
}
3化戳、設(shè)置代理
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.mainScrollView)
{
CGFloat contentOffsetY = scrollView.contentOffset.y;
CGFloat maxOffsetY = self.topViewHeight - self.floatHeight;
if (contentOffsetY >= maxOffsetY)
{
self.contentCanMove = YES;
self.canMove = NO; // 自己不能滑動(dòng)了
}
if (!self.canMove)
{
[scrollView setContentOffset:CGPointMake(0, maxOffsetY)];
}
}
else if ([scrollView isKindOfClass:UITableView.class])
{
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY <= 0)
{
self.canMove = YES;
self.contentCanMove = NO;
}
if (!self.contentCanMove)
{
[scrollView setContentOffset:CGPointMake(0, 0)];
}
}
}