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

相关推荐
Macle_Chen18 分钟前
ios版本的Tiktok二次安装不上,提示:Unable to Install “TikTok”
ios·职场和发展·蓝桥杯
解压专家66620 分钟前
Mac/iOS 如何解压 RAR 格式压缩包:常用工具与详细操作步骤
macos·ios
依旧003 小时前
react native webview加载本地HTML,解决iOS无法加载成功问题
react native·ios·html·vite·webview
Digitally4 小时前
如何轻松地将文件从 PC 传输到 iPhone?
ios·iphone
Digitally7 小时前
从 iPhone 备份照片: 保存iPhone图片的5种方法
ios·iphone
Digitally7 小时前
如何有效删除 iPhone 上的所有内容?
ios·iphone
90后的晨仔13 小时前
xcode 16.2报错 Sandbox: rsync.samba(xxxx)解决方案
ios
Digitally15 小时前
如何轻松地将数据从 iPhone传输到iPhone 16
ios·iphone
I烟雨云渊T16 小时前
iOS 电子书听书功能的实现
macos·ios·cocoa
二流小码农17 小时前
鸿蒙开发:UI界面分析利器ArkUI Inspector
android·ios·harmonyos