scrollview tableView嵌套解決方案示例
正文
在網(wǎng)上找了很多,沒有喜歡的方案。也參考了眾多設(shè)計,做了一款自認為比較簡潔、完美的方案:
大致思路:外層放置scrollview作為容器,容器內(nèi)上部分topView,下部分tableView。當tableView滾動時,如果topView還在展示區(qū)域,就設(shè)置topView的y坐標,讓topView跟隨同步上移。
(注意:如果不設(shè)置tableView的headerView,tableView、和topView都會同時上移不是我想要的效果,所以設(shè)置tableView的headerView高度包括topView的高度,達到了完美的效果,具體實現(xiàn)看demo)
效果預(yù)覽:

NestScrollView.gif
核心代碼就是在父視圖、子試圖的滾動判斷
//父視圖滾動的回調(diào),用于橫向滾動判斷
//父視圖滾動的回調(diào),用于橫向滾動判斷
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat placeholderOffset = 0;
if (self.topView.selectedIndex == 0) {
if (self.firstTableView.contentOffset.y > CGRectGetHeight(self.topView.frame) - kItemheight) {
placeholderOffset = CGRectGetHeight(self.topView.frame) - kItemheight;
}else {
placeholderOffset = self.firstTableView.contentOffset.y;
}
[self.secondTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
}else {
if (self.secondTableView.contentOffset.y > CGRectGetHeight(self.topView.frame) - kItemheight) {
placeholderOffset = CGRectGetHeight(self.topView.frame) - kItemheight;
}else {
placeholderOffset = self.secondTableView.contentOffset.y;
}
[self.firstTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
}
}
//子視圖滾動的回調(diào),用于豎直方向上滾動判斷
//子視圖滾動的回調(diào),用于豎直方向上滾動判斷
- (void)updateTopViewFrame:(UIScrollView *)scrollView{
CGFloat placeHolderHeight = CGRectGetHeight(self.topView.frame) - self.topView.itemHeight;
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat y = 0.0;
if (offsetY >= 0 && (offsetY <= placeHolderHeight)) {
y = -offsetY;
} else if (offsetY > placeHolderHeight) {
y = -placeHolderHeight;
} else if (offsetY < 0) {
y = -offsetY;
}
[self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.offset(y + kNavBarHeight);
}];
}
githut demo下載地址:https://github.com/biyuhuaping/NestScrollView
以上就是scrollview tableView嵌套解決方案示例的詳細內(nèi)容,更多關(guān)于scrollview tableView嵌套的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS實現(xiàn)簡易的導(dǎo)航欄顏色漸變實例代碼
很多APP 都有導(dǎo)航欄顏色漸變的效果,下面這篇文章主要給大家介紹了關(guān)于iOS如何實現(xiàn)簡易的導(dǎo)航欄顏色漸變效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧2018-10-10
Flutter Widgets MediaQuery控件屏幕信息適配
這篇文章主要為大家介紹了Flutter Widgets 之 MediaQuery控件獲取屏幕信息和屏幕適配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
iOS開發(fā)中使用文字圖標iconfont的應(yīng)用示例
這篇文章主要介紹了iOS開發(fā)中使用文字圖標iconfont的應(yīng)用示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
iOS NSURLSessionDownloadTask設(shè)置代理文件下載的示例
本篇文章主要介紹了iOS NSURLSessionDownloadTask設(shè)置代理文件下載的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

