scrollview tableView嵌套解决方案示例
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>正文</li><li>效果预览:</li><ul class="second_class_ul"><li>核心代码就是在父视图、子试图的滚动判断</li></ul></ul></div><p class="maodian"></p><h2>正文</h2><p>在网上找了很多,没有喜欢的方案。也参考了众多设计,做了一款自认为比较简洁、完美的方案:</p>
<p>大致思路:外层放置scrollview作为容器,容器内上部分topView,下部分tableView。当tableView滚动时,如果topView还在展示区域,就设置topView的y坐标,让topView跟随同步上移。</p>
<p>(注意:如果不设置tableView的headerView,tableView、和topView都会同时上移不是我想要的效果,所以设置tableView的headerView高度包括topView的高度,达到了完美的效果,具体实现看demo)</p>
<p class="maodian"></p><h2>效果预览:</h2>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202208/20220831084646028.gif" /></p>
<p>NestScrollView.gif</p>
<p class="maodian"></p><h3>核心代码就是在父视图、子试图的滚动判断</h3>
<p>//父视图滚动的回调,用于横向滚动判断</p>
<div class="jb51code"><pre class="brush:cpp;">//父视图滚动的回调,用于横向滚动判断
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat placeholderOffset = 0;
if (self.topView.selectedIndex == 0) {
if (self.firstTableView.contentOffset.y &gt; CGRectGetHeight(self.topView.frame) - kItemheight) {
placeholderOffset = CGRectGetHeight(self.topView.frame) - kItemheight;
}else {
placeholderOffset = self.firstTableView.contentOffset.y;
}
;
}else {
if (self.secondTableView.contentOffset.y &gt; CGRectGetHeight(self.topView.frame) - kItemheight) {
placeholderOffset = CGRectGetHeight(self.topView.frame) - kItemheight;
}else {
placeholderOffset = self.secondTableView.contentOffset.y;
}
;
}
}
</pre></div>
<p>//子视图滚动的回调,用于竖直方向上滚动判断</p>
<div class="jb51code"><pre class="brush:cpp;">//子视图滚动的回调,用于竖直方向上滚动判断
- (void)updateTopViewFrame:(UIScrollView *)scrollView{
CGFloat placeHolderHeight = CGRectGetHeight(self.topView.frame) - self.topView.itemHeight;
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat y = 0.0;
if (offsetY &gt;= 0 &amp;&amp; (offsetY &lt;= placeHolderHeight)) {
y = -offsetY;
} else if (offsetY &gt; placeHolderHeight) {
y = -placeHolderHeight;
} else if (offsetY &lt; 0) {
y = -offsetY;
}
[self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.offset(y + kNavBarHeight);
}];
}
</pre></div>
<p>githut demo下载地址:https://github.com/biyuhuaping/NestScrollView</p>
<p>以上就是scrollview tableView嵌套解决方案示例的详细内容,更多关于scrollview tableView嵌套的资料请关注琼殿技术社区其它相关文章!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件</li><li>判断 ScrollView List 是否正在滚动详解</li><li>UIScrollView实现六棱柱图片浏览效果</li><li>Android使用NestedScrollView 内嵌RecycleView滑动冲突问题解决</li><li>Android超详细讲解组件ScrollView的使用</li><li>Flutter滚动组件之SingleChildScrollView使用详解</li><li>一行代码教你解决Scrollview和TextInput焦点获取问题</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]