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滑动吸顶

相关推荐
tangbin5830851 小时前
iOS Swift 可选值(Optional)详解
前端·ios
卷心菜加农炮13 小时前
基于Python的FastAPI后端开发框架如何使用PyInstaller 进行打包与部署
ios
北极象1 天前
千问大模型接入示例
ios·iphone·qwen
ipad协议开发1 天前
企业微信 iPad 协议应用机器人开发
ios·企业微信·ipad
QuantumLeap丶2 天前
《Flutter全栈开发实战指南:从零到高级》- 26 -持续集成与部署
android·flutter·ios
2501_915918412 天前
TCP 抓包分析在复杂网络问题中的作用,从连接和数据流层面理解系统异常行为
网络·网络协议·tcp/ip·ios·小程序·uni-app·iphone
二流小码农2 天前
鸿蒙开发:个人开发者如何使用华为账号登录
android·ios·harmonyos
wvy2 天前
Xcode 26还没有适配SceneDelegate的app建议尽早适配
ios
游戏开发爱好者82 天前
苹果 App 上架流程,结合 Xcode、CI 等常见工具
macos·ios·ci/cd·小程序·uni-app·iphone·xcode
前端老白2 天前
webview在微信小程序中,安卓加载失败,IOS正常加载
android·ios·微信小程序·webview