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

相关推荐
库奇噜啦呼4 分钟前
【iOS】懒加载
macos·objective-c·cocoa
敲代码的鱼哇3 小时前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨4 小时前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨4 小时前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos
鹏多多.5 小时前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios
他们都不看好你,偏偏你最不争气7 小时前
【iOS】多界面传值
ios
MaoJiu1 天前
Flutter混合开发:在iOS工程中嵌入Flutter Module
flutter·ios
2501_915921431 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008891 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
Digitally1 天前
如何将视频从 iPhone 转移到 Mac
macos·ios·iphone