UITableView视差效果的封装

关于tableView视差效果,我们来封装一下。</br>
具体实现思路请看:</br>

  1. http://www.jianshu.com/p/00f069c91bea</br>
  2. http://www.lrymlt.cn/blog/?p=109

一. 在工程中新建一个继承于UITableView的类,在.m中重写初始化方法。

在初始化方法中注册已经写好的cell,并设置self为代理人,签

    - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
    {
        self = [super initWithFrame:frame style:style];

        if (self) {
    
            [self registerClass:[LTParallaxCell class] forCellReuseIdentifier:@"mycell"];
    
            self.delegate   = self;
            self.dataSource = self;
    
            self.separatorStyle = UITableViewCellSeparatorStyleNone;
            // 设置默认cell高度
            self.cellHeight     = 200;
            // 设置默认imageView高度
            self.imageHeight    = 300;
        }

        return self;
    }

二. 在.h文件中声明几个属性,方便用户对样式做基本设置。

    /** 数据源数组,存储图片名称或地址 */
    @property (nonatomic, strong) NSArray *sourceArray;

    /** 数据源数组 */
    @property (nonatomic, strong) NSArray *titleSourceArray;

    /** cell的高度 */
    @property (nonatomic, assign) CGFloat cellHeight;

    /** 图片高度 */
    @property (nonatomic, assign) CGFloat imageHeight;

    /** 是否包含标题 */
    @property (nonatomic, assign) BOOL isHasTitle;

    /** 标题背景色 */
    @property (nonatomic, strong) UIColor *titleBackgroundColor;

    /** 标题文字颜色 */
    @property (nonatomic, strong) UIColor *titleTextColor;

三. 实现几个代理方法

    - (void) updateImageViewOffsetOfCell:(LTParallaxCell *)cell
    {
        // 获取当前cell的偏移量
        CGFloat cellY = cell.frame.origin.y - self.contentOffset.y;
        // 计算图片最大的偏移量范围
        CGFloat imgMaxHeight = [cell imageOverflowHeight];
        // 计算图片偏移量
        CGFloat offset = 0.0f - imgMaxHeight * cellY / self.frame.size.height;

        [cell setImageOffset:CGPointMake(0.0f, offset)];
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return _cellHeight;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _sourceArray.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        LTParallaxCell *cell        = [self dequeueReusableCellWithIdentifier:@"mycell"];

        cell.mainImageView.image    = [UIImage imageNamed:[_sourceArray objectAtIndex:indexPath.row]];

        cell.selectionStyle         = UITableViewCellSelectionStyleNone;

        cell.clipsToBounds          = YES;

        cell.imageViewHeight        = _imageHeight;

        if (_isHasTitle != 0) {
    
            cell.isHasTitle = _isHasTitle;
    
        }

        if (_titleSourceArray != nil) {
    
            cell.titleLabel.text = [_titleSourceArray objectAtIndex:indexPath.row];
    
        }

        if (_titleBackgroundColor != nil) {
    
            cell.titleBackgroundColor = _titleBackgroundColor;
    
        }

        return cell;
    }

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self updateImageViewOffsetOfCell:(LTParallaxCell *)cell];
    }

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        for (LTParallaxCell *cell in self.visibleCells) {
    
            [self updateImageViewOffsetOfCell:cell];
    
        }
    }

四. 在原有cell基础上增加几个属性并重写set方法

    /** 标题背景色 */
    @property (nonatomic, strong) UIColor *titleBackgroundColor;

    /** 标题文字颜色 */
    @property (nonatomic, strong) UIColor *titleTextColor;

    /** 图片高度 */
    @property (nonatomic, assign) CGFloat imageViewHeight;

    /** cell高度 */
    @property (nonatomic, assign) CGFloat cellHeight;

set方法实现如下:

    /**
     *  isHasTitle的set方法
     *
     *  @param isHasTitle
     */
    - (void)setIsHasTitle:(BOOL)isHasTitle
    {
        if (_isHasTitle != isHasTitle) {
    
            _isHasTitle = isHasTitle;
    
            // 判断有标题,则将titleLabel控件添加到图层中
            if (_isHasTitle) {
        
                _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, [UIScreen mainScreen].bounds.size.width, 40)];
        
                _titleLabel.text = @"这是图片上的文字";
        
                _titleLabel.textAlignment = 1;
        
                [self.contentView addSubview:_titleLabel];
        
            }
        }
    }

    - (void)setTitleBackgroundColor:(UIColor *)titleBackgroundColor
    {
        if (_titleBackgroundColor != titleBackgroundColor) {
    
            _titleBackgroundColor = titleBackgroundColor;
    
        }

        _titleLabel.backgroundColor = titleBackgroundColor;
    }

    /**
    *  设置图片高度
    *
    *  @param imageViewHeight 图片高度
    */
    -(void)setImageViewHeight:(CGFloat)imageViewHeight
    {
        if (_imageViewHeight != imageViewHeight) {
    
            _imageViewHeight = imageViewHeight;
    
            // 将图片高度修改为用户设置
            _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , _imageViewHeight)];
    
        }
    }

封装好的代码地址:https://github.com/menglingtong/LTParallaxTableView/tree/master

五. 调用方法
使用起来就很简单了,将头文件引入到VC中,按照我们开放好的接口挨个设置就好啦!例如:

LTParallaxTableView *tableView = [[LTParallaxTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

// 设置包含标题
tableView.isHasTitle        = YES;
// 将图片数组传入
    tableView.sourceArray       = sourceArr;
// 将标题数组传入
tableView.titleSourceArray  = titleSourceArr;
// 设置cell的高度
tableView.cellHeight        = 200;
// 设置imageView的高度
tableView.imageHeight       = 300;
// 设置标题背景颜色
tableView.titleBackgroundColor = [UIColor colorWithRed:0.36 green:0.72 blue:0.33 alpha:0.40];

[self.view addSubview:tableView];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,109评论 4 62
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 10,993评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,673评论 18 139
  • 作者碎碎念:此乃世纪大坑 慢慢填 一篇文章三到四首歌 不专业,不会写乐评,正在努力学习中。以小故事和听后感为主。 ...
    舒嘉仪阅读 610评论 0 0
  • 孟河是一个村子,不是一条河。但确实有一条河绕了大半个村子,这条河没有名字,村子里最老的老人也从没提过它的名字...
    月亮像银子阅读 881评论 2 1