ios tableview吸顶

由于项目需要实现一个上滑吸顶的效果,网上也看到有很多种方式实现,但是如果加上下拉刷新的功能会导致界面异常,还有第三方库实现方式库,太繁琐了,下面是我的实现方式,效果如下:

tablevie滑动吸顶

主要代码部分:

横线滚动代理方法updateTopViewFrame

复制代码
//父视图滚动的回调,用于横向滚动判断
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidScroll - %.2f, %.2f",scrollView.contentOffset.x, scrollView.contentOffset.y);
    
    CGFloat placeholderOffset = 0;
    UIScrollView *scroll;
    if (self.topView.selectedIndex == 0) {
        scroll = self.firstTableView;
    }else {
        scroll = self.secondTableView;
    }
    if (scroll.contentOffset.y > CGRectGetHeight(self.topView.frame) - self.topView.itemHeight) {
        placeholderOffset = CGRectGetHeight(self.topView.frame) - self.topView.itemHeight;
    }else {
        placeholderOffset = scroll.contentOffset.y;
    }
    
    //当主talbeview向上滑动的时候,要禁止主scrollerview滑动,
    if([scrollView isKindOfClass:CustomScrollView.class]){
        if(scrollView.contentOffset.y >= 0){ //向上滑动
            self.ctyscrollview.contentOffset = CGPointZero;
            self.ctyscrollview.scrollEnabled = NO;
            self.firstTableView.contentOffset = CGPointZero;
            self.secondTableView.contentOffset = CGPointZero;
        }
        return;
    }
    
    [self.secondTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
    [self.firstTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
}

纵向滑动代理方法updateTopViewFrame

复制代码
//子视图滚动的回调,用于竖直方向上滚动判断
- (void)updateTopViewFrame:(UIScrollView *)scrollView{
    CGFloat placeHolderHeight = CGRectGetHeight(self.topView.frame) - self.topView.itemHeight;
    CGFloat offsetY = scrollView.contentOffset.y;
    
    //顶部topiview跟随滑动
    CGFloat y = 0.0;
    if (offsetY >= 0 && (offsetY <= placeHolderHeight)) {
        NSLog(@"1- offsetY:%.2f <= placeHolderHeight:%.2f", offsetY, placeHolderHeight);
        y = -offsetY;
    } else if (offsetY > placeHolderHeight) {
        NSLog(@"2- offsetY:%.2f > placeHolderHeight:%.2f", offsetY, placeHolderHeight);
        y = -placeHolderHeight;
    } else if (offsetY < 0) {
        NSLog(@"3- offsetY:%.2f < 0,  placeHolderHeight:%.2f", offsetY, placeHolderHeight);
        y = -offsetY;
    }
    
    self.topView.y = y;
    
    
    //当主scrollerview滑动的时候,tableview要禁止滑动,
    if(offsetY <= 0){
        self.ctyscrollview.scrollEnabled = YES;
        self.firstTableView.contentOffset = CGPointZero;
        self.secondTableView.contentOffset = CGPointZero;
        if(!CGPointEqualToPoint(self.ctyscrollview.frame.origin, CGPointZero)){
            self.ctyscrollview.contentOffset = CGPointZero;
        }
    }else{
        self.ctyscrollview.scrollEnabled = NO;
    }
}

CustomScrollView中需要加上以下联动代理方法

复制代码
///允许同时接受手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

demo代码下载地址:

Jack/多tableview滑动吸顶

相关推荐
大熊猫侯佩11 小时前
黑衣人档案:用 Apple Foundation Models + SwiftUI 打造 AI 聊天机器人全攻略
ios·swiftui·ai编程
大熊猫侯佩11 小时前
侠客行・iOS 26 Liquid Glass TabBar 破阵记
ios·swiftui·swift
2501_9160074713 小时前
手机使用过的痕迹能查到吗?完整查询指南与步骤
android·ios·智能手机·小程序·uni-app·iphone·webview
從南走到北15 小时前
JAVA国际版同城外卖跑腿团购到店跑腿多合一APP系统源码支持Android+IOS+H5
android·java·ios·微信小程序·小程序
alengan17 小时前
苹果企业签名流程
ios·iphone
Digitally1 天前
如何在Mac上同步iPhone短信
macos·ios·iphone
2501_915106321 天前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
2501_916008891 天前
金融类 App 加密加固方法,多工具组合的工程化实践(金融级别/IPA 加固/无源码落地/Ipa Guard + 流水线)
android·ios·金融·小程序·uni-app·iphone·webview
2501_915921431 天前
Fastlane 结合 开心上架(Appuploader)命令行版本实现跨平台上传发布 iOS App 免 Mac 自动化上架实战全解析
android·macos·ios·小程序·uni-app·自动化·iphone
游戏开发爱好者81 天前
iOS 上架要求全解析,App Store 审核标准、开发者准备事项与开心上架(Appuploader)跨平台免 Mac 实战指南
android·macos·ios·小程序·uni-app·iphone·webview