正文
在网上找了很多,没有喜欢的方案。也参考了众多设计,做了一款自认为比较简洁、完美的方案:
大致思路:外层放置scrollview作为容器,容器内上部分topview,下部分tableview。当tableview滚动时,如果topview还在展示区域,就设置topview的y坐标,让topview跟随同步上移。
(注意:如果不设置tableview的headerview,tableview、和topview都会同时上移不是我想要的效果,所以设置tableview的headerview高度包括topview的高度,达到了完美的效果,具体实现看demo)
效果预览:
nestscrollview.gif
核心代码就是在父视图、子试图的滚动判断
//父视图滚动的回调,用于横向滚动判断
//父视图滚动的回调,用于横向滚动判断 - (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
以上就是scrollview tableview嵌套解决方案示例的详细内容,更多关于scrollview tableview嵌套的资料请关注代码网其它相关文章!
发表评论