iOS分页控制器

作者: ytsq | 来源:发表于2017-09-30 16:57 被阅读0次

    刚换了一家新公司,接触的新项目,项目比较老,分页列表直接用的scrollView做的,添加好几个tableView,各种数据源代理,方法里面各种判断,下拉刷新,唉,头好疼.添加或者删除一个要花费好长时间,还容易出现问题.刚开始两个还好,后来让我改成四个,然后再改成三个,说以后功能全了再改成四个,还要各种换顺序(虽然我改成枚举了),瞬间泪崩,改成四个还是按照原来的思路继续再scrollView上添加,简单的一个数据源就要这样写,一个页面两千多行代码,无数if else

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        if (tableView == _matchTableView) {
            return _matchDataArray.count;
        }else if (tableView == self.gameLiveTableView) {
        
            return self.gameLiveDatas.count;
        }else if (tableView == self.footerLiveTableView) {
            return self.footerLiveDatas.count;
        }
        return [dataArym count];
    }
    

    后来是在忍不了,决定重写,现在网上找了一个框架,用collectionView做的分页,效果也实现了,但是有bug,不会每次都调用viewWillAppear方法,我的做法比较笨

    // 减速完成
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        CGFloat offsetX = scrollView.contentOffset.x;
        NSInteger offsetXInt = offsetX;
        NSInteger screenWInt = YZScreenW;
        
        NSInteger extre = offsetXInt % screenWInt;
        if (extre > YZScreenW * 0.5) {
            // 往右边移动
            offsetX = offsetX + (YZScreenW - extre);
            _isAniming = YES;
            [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        }else if (extre < YZScreenW * 0.5 && extre > 0){
            _isAniming = YES;
            // 往左边移动
            offsetX =  offsetX - extre;
            [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        }
        
        // 获取角标
        NSInteger i = offsetX / YZScreenW;
        
        // 选中标题
        [self selectLabel:self.titleLabels[i]];
        
        // 取出对应控制器发出通知
        UIViewController *vc = self.childViewControllers[i];
        [vc viewWillAppear:YES]; #red // 这里我给他手动调用viewWillAppear
        // 发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:YZDisplayViewClickOrScrollDidFinshNote object:vc];
    }
    

    这里我是手动调用viewWillAppear,弊端就是有些控制器会调用好两次这个方法,我试过,好像只有collectionView的第一个cell跟最后一个cell才不会调用,其他的是正常的,不知道是巧合还是就这样.但是其他的几个生命周期的方法也不是每次都走,框架里的做法是发送通知,功能也是实现了,控制器提取出来,代码变得好清爽

    //设置样式
     [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight, CGFloat *titleWidth) {
            *norColor = [SharedMethod getColorByHexString:@"333333"];
            *selColor = [SharedMethod getColorByHexString:@"F95133"];
            *titleWidth = SCALE_X(65);
            *titleScrollViewColor = [UIColor whiteColor];
            *titleFont = YFYUIFontBy750(17);
        }];
    //创建三个控制器
        CS_LiveListViewController *hotVC = [CS_LiveListViewController new];
        hotVC.title = @"热门";
        [hotVC addNotification];
        hotVC.liveListType = kLiveListTypeHot;
        hotVC.showFotter = NO;
        hotVC.showBanner = YES;
        [self addChildViewController:hotVC];
    
        CS_LiveListViewController *gameVC = [CS_LiveListViewController new];
        gameVC.title = @"游戏";
        gameVC.liveListType = kLiveListTypeGame;
        gameVC.showFotter = YES;
        gameVC.showBanner = YES;
        [self addChildViewController:gameVC];
    
        CS_LiveListViewController *funVC = [CS_LiveListViewController new];
        funVC.title = @"赛事";
        funVC.showFotter = NO;
        funVC.liveListType = kLiveListTypeMatch;
        funVC.showBanner = YES;
        [self addChildViewController:funVC];
    //刷新
        [self refreshDisplay];
    

    毕竟不完美,强迫症犯了,一直想搞个完美的,后来发现了一个大神写的分页控制器,号称企鹅FM的(里面说明了UIPageController的弊端),好吧,很强.果断上手,用了一下非常流畅,生命周期完美,源码我也在学习中,然后封装了其中一部分功能,使用起来非常简单.

    #import "SPTabController.h"
    
    typedef NS_ENUM(NSInteger, FKPageViewControllerMarkViewType) {
        FKPageViewControllerMarkViewTypeEquWidthLine = 0,//等宽
        FKPageViewControllerMarkViewTypeAdjustWidthLine,//根据标题调整宽度
        FKPageViewControllerMarkViewTypePoint ,//点
        FKPageViewControllerMarkViewTypeNone//没有
    };
    
    @interface FKPageViewController : SPTabController
    
    /**
     必须要传  要不然会崩溃
     */
    @property (nonatomic, strong) NSArray<UIViewController *> *controllers;
    
    /**
     markView类型 默认等宽
     */
    @property (nonatomic, assign) FKPageViewControllerMarkViewType markViewType;
    
    /**
     markView 颜色 默认橘色
     */
    @property (nonatomic, strong) UIColor *markViewColor;
    
    /**
     标题正常颜色 默认黑色
     */
    @property (nonatomic, strong) UIColor *titleNormalColor;
    
    /**
     标题选中颜色 默认橘色
     */
    @property (nonatomic, strong) UIColor *titleSelectedColor;
    
    /**
     未选中的时候的字体大小 默认15号字体
     */
    @property (nonatomic, assign) NSInteger normalFont;
    
    /**
     选中状态下大小 默认等于正常
     */
    @property (nonatomic, assign) NSInteger selectedFont;
    
    /**
     markView距离底部的距离 你只需要传一个正数
     */
    @property (nonatomic, assign) CGFloat markViewY;
    
    /**
     标题栏开始的位置
     */
    @property (nonatomic, assign) CGFloat preferTiltleY;
    
    /**
     是否需要在标题下面增加一条线
     */
    @property (nonatomic, assign) BOOL needTitleBottomLine;
    
    /**
     标题底部线的颜色
     */
    @property (nonatomic, strong) UIColor *titleBottomLineColor;
    
    /**
     标题栏背景颜色 默认透明
     */
    @property (nonatomic, strong) UIColor *titleBackgroudColor;
    
    @end
    

    因为里面做了一些修改,增加了一小部分功能跟不完善的地方,所以不建议使用pod安装,可以下载直接拖进项目.使用非常方便,注释也都添加了,使用流畅的一塌糊涂 ~~
    SPPage 大家可以学习一下,框架还有其他的功能,感觉暂时用不到,还没仔细看
    最后是我封装的demo

    相关文章

      网友评论

        本文标题:iOS分页控制器

        本文链接:https://www.haomeiwen.com/subject/uwnvrxtx.html