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

相关推荐
江东小bug王4 小时前
深入解析 iOS 与 macOS 应用程序生命周期(完整指南)
macos·ios
2501_916008897 小时前
iOS 发布全流程详解,从开发到上架的流程与跨平台使用 开心上架 发布实战
android·macos·ios·小程序·uni-app·cocoa·iphone
非专业程序员9 小时前
iOS/Swift:深入理解iOS CoreText API
ios·swift
某柚啊10 小时前
iOS移动端H5键盘弹出时页面布局异常和滚动解决方案
前端·javascript·css·ios·html5
RollingPin21 小时前
iOS八股文之 RunLoop
ios·多线程·卡顿·ios面试·runloop·ios保活·ios八股文
2501_916007471 天前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
LinXunFeng1 天前
Flutter webview 崩溃率上升怎么办?我的分析与解决方案
flutter·ios·webview
游戏开发爱好者81 天前
FTP 抓包分析实战,命令、被动主动模式要点、FTPS 与 SFTP 区别及真机取证流程
运维·服务器·网络·ios·小程序·uni-app·iphone
Nick56831 天前
Xcode16 避坑
ios
ii_best1 天前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器