当前位置:网站首页>scrollview、tableView嵌套解决方案
scrollview、tableView嵌套解决方案
2022-07-27 22:08:00 【碧羽化屏】
scrollview、tableView嵌套解决方案
在网上找了很多,没有喜欢的方案。也参考了众多设计,做了一款自认为比较简洁、完美的方案:
大致思路:外层放置scrollview作为容器,容器内上部分topView,下部分tableView。当tableView滚动时,如果topView还在展示区域,就设置topView的y坐标,让topView跟随同步上移。
(注意:如果不设置tableView的headerView,tableView、和topView都会同时上移不是我想要的效果,所以设置tableView的headerView高度包括topView的高度,达到了完美的效果,具体实现看demo)
效果预览:

核心代码就是在父视图、子试图的滚动判断
//父视图滚动的回调,用于横向滚动判断
//父视图滚动的回调,用于横向滚动判断
- (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];
}
}
//子视图滚动的回调,用于竖直方向上滚动判断
//子视图滚动的回调,用于竖直方向上滚动判断
- (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
边栏推荐
- Buildforge materials
- 基于Unittest的ddt+yaml实现数据驱动机制
- Y79. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (10)
- 半导体测试设备市场现状:国产化率仍不足10%!
- 网络设备硬核技术内幕 防火墙与安全网关篇 (六) 安全双修大法 下
- Build Release Blogs
- Set 数据构造函数
- Volkswagen China invested 8billion yuan and became the largest shareholder of GuoXuan high tech
- 一文读懂CMake
- Rational and perceptual activities and required skills in programmers' work
猜你喜欢

Matlab | those matlab tips you have to know (I)
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/17/15c759aadafc335028d37380903ee7.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

迷惑的单片机矩阵按键

Matlab | those matlab tips you have to know (4)

推进云网融合,筑路数字经济:英特尔亮相第五届数字中国建设峰会-云生态大会

How does matlab set the K-line diagram to classic red and green color matching?
![[leetcode] 547. Number of provinces (medium)](/img/15/d49d18151c47e318fe7acabdd616e6.png)
[leetcode] 547. Number of provinces (medium)

数据分析:拆解方法(详情整理)

图片提取文字很神奇?试试三步实现OCR!

Point divide and conquer analysis
随机推荐
Set 数据构造函数
LeetCode_ Bit operation_ Medium_ 137. Number II that appears only once
Build Release Blogs
Build Release Blogs
自动推理的逻辑09–自动定理证明
英特尔携手汉朔、微软,释放“AI + 零售”大招!
Matlab | those matlab tips you have to know (2)
In July, a software testing engineer came to the company. He looked like a hairy boy. He didn't expect to be the new generation of roll King
Count the six weapons of the domestic interface cooperation platform!
从第二层到第三层
Interesting Huffman tree
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
加拿大法院认定孟晚舟“双重犯罪”成立,引渡程序将继续进行!
Jerry Zhi doesn't play hidden audio files [article]
The latest notice of the Chinese Academy of Sciences: abandon the impact factor! The journal zoning table will be published for the "Journal surpassing index"
网络设备硬核技术内幕 防火墙与安全网关篇 (六) 安全双修大法 下
Rational and perceptual activities and required skills in programmers' work
In the first quarter of 2020, the wearable market shipped 72.6 million units, with apple occupying nearly 30% of the market share
Jerry's PWM setting and PWM IO selection [chapter]
Firefox 103, the Firefox browser, has been released to improve performance under high refresh rate displays