当前位置: 代码网 > it编程>App开发>苹果IOS > scrollview tableView嵌套解决方案示例

scrollview tableView嵌套解决方案示例

2024年05月19日 苹果IOS 我要评论
正文在网上找了很多,没有喜欢的方案。也参考了众多设计,做了一款自认为比较简洁、完美的方案:大致思路:外层放置scrollview作为容器,容器内上部分topview,下部分tableview。当tab

正文

在网上找了很多,没有喜欢的方案。也参考了众多设计,做了一款自认为比较简洁、完美的方案:

大致思路:外层放置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嵌套的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com