UITableView視差效果的封裝

關(guān)于tableView視差效果,我們來封裝一下。</br>
具體實現(xiàn)思路請看:</br>

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

一. 在工程中新建一個繼承于UITableView的類,在.m中重寫初始化方法。

在初始化方法中注冊已經(jīng)寫好的cell,并設(shè)置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;
            // 設(shè)置默認(rèn)cell高度
            self.cellHeight     = 200;
            // 設(shè)置默認(rèn)imageView高度
            self.imageHeight    = 300;
        }

        return self;
    }

二. 在.h文件中聲明幾個屬性,方便用戶對樣式做基本設(shè)置。

    /** 數(shù)據(jù)源數(shù)組,存儲圖片名稱或地址 */
    @property (nonatomic, strong) NSArray *sourceArray;

    /** 數(shù)據(jù)源數(shù)組 */
    @property (nonatomic, strong) NSArray *titleSourceArray;

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

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

    /** 是否包含標(biāo)題 */
    @property (nonatomic, assign) BOOL isHasTitle;

    /** 標(biāo)題背景色 */
    @property (nonatomic, strong) UIColor *titleBackgroundColor;

    /** 標(biāo)題文字顏色 */
    @property (nonatomic, strong) UIColor *titleTextColor;

三. 實現(xiàn)幾個代理方法

    - (void) updateImageViewOffsetOfCell:(LTParallaxCell *)cell
    {
        // 獲取當(dāng)前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基礎(chǔ)上增加幾個屬性并重寫set方法

    /** 標(biāo)題背景色 */
    @property (nonatomic, strong) UIColor *titleBackgroundColor;

    /** 標(biāo)題文字顏色 */
    @property (nonatomic, strong) UIColor *titleTextColor;

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

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

set方法實現(xiàn)如下:

    /**
     *  isHasTitle的set方法
     *
     *  @param isHasTitle
     */
    - (void)setIsHasTitle:(BOOL)isHasTitle
    {
        if (_isHasTitle != isHasTitle) {
    
            _isHasTitle = isHasTitle;
    
            // 判斷有標(biāo)題,則將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;
    }

    /**
    *  設(shè)置圖片高度
    *
    *  @param imageViewHeight 圖片高度
    */
    -(void)setImageViewHeight:(CGFloat)imageViewHeight
    {
        if (_imageViewHeight != imageViewHeight) {
    
            _imageViewHeight = imageViewHeight;
    
            // 將圖片高度修改為用戶設(shè)置
            _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , _imageViewHeight)];
    
        }
    }

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

五. 調(diào)用方法
使用起來就很簡單了,將頭文件引入到VC中,按照我們開放好的接口挨個設(shè)置就好啦!例如:

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

// 設(shè)置包含標(biāo)題
tableView.isHasTitle        = YES;
// 將圖片數(shù)組傳入
    tableView.sourceArray       = sourceArr;
// 將標(biāo)題數(shù)組傳入
tableView.titleSourceArray  = titleSourceArr;
// 設(shè)置cell的高度
tableView.cellHeight        = 200;
// 設(shè)置imageView的高度
tableView.imageHeight       = 300;
// 設(shè)置標(biāo)題背景顏色
tableView.titleBackgroundColor = [UIColor colorWithRed:0.36 green:0.72 blue:0.33 alpha:0.40];

[self.view addSubview:tableView];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,366評論 4 61
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,423評論 6 13
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 作者碎碎念:此乃世紀(jì)大坑 慢慢填 一篇文章三到四首歌 不專業(yè),不會寫樂評,正在努力學(xué)習(xí)中。以小故事和聽后感為主。 ...
    舒嘉儀閱讀 771評論 0 0
  • 孟河是一個村子,不是一條河。但確實有一條河繞了大半個村子,這條河沒有名字,村子里最老的老人也從沒提過它的名字...
    月亮像銀子閱讀 1,066評論 2 1

友情鏈接更多精彩內(nèi)容