iOS 淘宝首页、电商首页 瀑布流效果 复杂collectionView样式

最近需求中需要对首页进行改版,插入瀑布流效果,但是我的首页是collectionView创建的,里面布局太多了,不好修改,以前遇到这样的需求,当时直接想的不同的second设置不同的layout ,但是感觉太复杂了,对我来说有点难度,后面想到把上面的那部分全部设置为表头,只留一个瀑布流的layout实现,但是对于我们项目来说,这样写改动太大了,我还怕改出问题,所以就有了以下方式,先看下具体效果:

gif.gif

实现思路

  • 瀑布流的那个效果单独做一个自定义的cell,里面设置瀑布流效果
  • 难度就是计算高度的问题,这个cell的高度需要去计算,每次获取新数据的时候,都把所有数据遍历算一遍当前数据所瀑布流布局得到的高度
  • 计算高度的时候要知道,因为我的是两列,所以 我把数组两个两个变成一个组合 组成大数组,然后遍历,计算左边高度 和 右边高度,相比较,哪边矮就先把数据布局到矮的那边,这样就可以得到最终的左右两边高度,当然记得加上行间距,然后再取一下左右高度最大值得到整个瀑布流的高度。

实现代码

//为什么要加号方法,因为我的cell高度计算是通过cell加号方法根据数据计算的,所以下面用的都是加号方法,
// 拆分数组变成2个2个一组
+ (NSArray *)splitArray:(NSArray *)originalArray withSubSize:(NSInteger)subSize
{
    NSInteger count = originalArray.count % subSize == 0 ? (originalArray.count / subSize) : (originalArray.count / subSize + 1);
    NSMutableArray *newArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < count; i ++) {
        NSInteger j = i * subSize;
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        while (j < subSize*(i + 1) && j < originalArray.count) {
            [tempArray addObject:[originalArray objectAtIndex:j]];
            j += 1;
        }
        [newArray addObject:[tempArray copy]];
    }
    return [newArray copy];
}

+(CGFloat)caculateEndHeight:(OSFlowVideoModel *)model
{

    CGFloat height = 0.0;
    //左边的高度
    CGFloat leftHeight = 0.0;
    //右边的高度
    CGFloat rightHeight = 0.0;
    //最终高度
    CGFloat contentHeight = 0.0;
    //数组两个两个组成数据
    NSArray *array = [self splitArray:model.list withSubSize:2];
    for (int i = 0; i<array.count; i++) {
        NSArray *arr = array[i];
        for (int j = 0; j<2; j++) {
            OSFlowVideoItemModel *a = arr[j];
            if (leftHeight > rightHeight) {
                rightHeight = rightHeight + [self caculateContentHeight:a] + PtOnIPhone6(7);
            }else{
                leftHeight = leftHeight + [self caculateContentHeight:a] + PtOnIPhone6(7);
            }
        }
    }
    contentHeight =  MAX(leftHeight, rightHeight);
    //多加了一个间距
    height -= PtOnIPhone6(7);
    height += contentHeight;
    return height;
}
//计算每个item内容高度
+(CGFloat)caculateContentHeight:(OSFlowVideoItemModel *)item
{
    CGFloat itemWidth = (OSScreenWidth - PtOnIPhone6(32) - PtOnIPhone6(7))/2;
    CGFloat height = 0.0;
    CGFloat titleH = [OSUtiltity getHeightWithText:item.title width:itemWidth - PtOnIPhone6(12) fontSize:RegularSysFont(14) withLimitHeight:PtOnIPhone6(40)];
    CGFloat imageHeight = 0.0;
    if (item.width.floatValue > item.height.floatValue) {
        imageHeight = itemWidth*3/4;
    }else if (item.width.floatValue < item.height.floatValue){
        imageHeight = itemWidth*4/3;
    }else{
        imageHeight = itemWidth;
    }
    height = titleH + imageHeight + PtOnIPhone6(16) + PtOnIPhone6(11) + PtOnIPhone6(20) + PtOnIPhone6(16);
    return height;
}
//计算的是cell高度,根据自身的情况来
+ (CGFloat)heightWithItems:(NSArray<OSStyleBaseItem *> *)items indexPath:(NSIndexPath *)indexPath width:(CGFloat)width
{
    OSFlowVideoModel *model = (OSFlowVideoModel *)items[indexPath.row];
    return  [self caculateEndHeight:model];
}

瀑布流的collectionViewlayout

  • h文件
@class EWWaterFallLayout;
 
@protocol EWWaterFallLayoutDataSource<NSObject>
 
@required
/**
  * 每个item的高度
  */
- (CGFloat)waterFallLayout:(EWWaterFallLayout *)waterFallLayout heightForItemAtIndexPath:(NSUInteger)indexPath itemWidth:(CGFloat)itemWidth;
 
@optional
/**
 * 有多少列
 */
- (NSUInteger)columnCountInWaterFallLayout:(EWWaterFallLayout *)waterFallLayout;
 
/**
 * 每列之间的间距
 */
- (CGFloat)columnMarginInWaterFallLayout:(EWWaterFallLayout *)waterFallLayout;
 
/**
 * 每行之间的间距
 */
- (CGFloat)rowMarginInWaterFallLayout:(EWWaterFallLayout *)waterFallLayout;
/**
* 每个item的内边距
*/
- (UIEdgeInsets)edgeInsetsInWaterFallLayout:(EWWaterFallLayout *)waterFallLayout;

@end
                   
@interface EWWaterFallLayout : UICollectionViewLayout

/**
* 代理
*/
@property (nonatomic, weak) id<EWWaterFallLayoutDataSource> delegate;

@end

-m文件


/** 默认的列数 */
static const CGFloat EWDefaultColumnCount = 2;
/** 每一列之间的间距 */
static const CGFloat EWDefaultColumnMargin = 7;
/** 每一行之间的间距 **/
static const CGFloat EWDefaultFRowMargin = 7;
/** 内边距 */
static const UIEdgeInsets EWDefaultEdgeInsets = {0,0,0,0};

@interface EWWaterFallLayout()
/** 存放所有的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArr;
/** 存放所有列的当前高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 内容的高度 */
@property (nonatomic, assign) CGFloat contentHeight;

- (NSUInteger)columnCount;
- (CGFloat)columnMargin;
- (CGFloat)rowMargin;
- (UIEdgeInsets)edgeInsets;

@end
@implementation EWWaterFallLayout
- (NSMutableArray *)attrsArr {
    if (!_attrsArr) {
        _attrsArr = [NSMutableArray array];
    }
    return _attrsArr;
}
 
- (NSMutableArray *)columnHeights {
    if (!_columnHeights) {
        _columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}
 
#pragma mark 数据处理
/**
  * 列数
 */
- (NSUInteger)columnCount {
    if ([self.delegate respondsToSelector:@selector(columnCountInWaterFallLayout:)]) {
        return [self.delegate columnCountInWaterFallLayout:self];
    } else {
        return EWDefaultColumnCount;
    }
}

/**
 * 列间距
 */
- (CGFloat)columnMargin {
    if ([self.delegate respondsToSelector:@selector(columnMarginInWaterFallLayout:)]) {
        return [self.delegate columnMarginInWaterFallLayout:self];
    } else {
        return EWDefaultColumnMargin;
    }
}
 
/**
 * 行间距
 */
- (CGFloat)rowMargin {
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFallLayout:)]) {
        return [self.delegate rowMarginInWaterFallLayout:self];
    } else {
        return EWDefaultFRowMargin;
    }
}

/**
 * item的内边距
 */
- (UIEdgeInsets)edgeInsets {
    if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterFallLayout:)]) {
        return [self.delegate edgeInsetsInWaterFallLayout:self];
    } else {
        return EWDefaultEdgeInsets;
    }
}
 
/**
 * 初始化
 */
- (void)prepareLayout {
    [super prepareLayout];
    
    self.contentHeight = 0;
    
    //清除之前计算的所有高度
    [self.columnHeights removeAllObjects];
    
    //设置每一列默认的高度
    for (NSInteger i = 0; i < self.columnCount; i++) {
        [self.columnHeights addObject:@(EWDefaultEdgeInsets.top)];
    }
    //清除之前所有的布局属性
        [self.attrsArr removeAllObjects];
        
        //开始创建每一个cell对应的布局属性
        NSInteger count = [self.collectionView numberOfItemsInSection:0];
        
        for (int i = 0; i < count; i++) {
            
            //创建位置
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            
            //获取indexPath位置上cell对应的布局属性
            UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
            
            [self.attrsArr addObject:attrs];
        }
    }
/**
 * 返回indexPath位置cell对应的布局属性
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //创建布局属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    //collectionView的宽度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    //设置布局属性的frame
    CGFloat cellW = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
    CGFloat cellH = [self.delegate waterFallLayout:self heightForItemAtIndexPath:indexPath.item itemWidth:cellW];
    //找出最短的那一列
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
    for (int i = 0; i < self.columnCount; i++) {
        //取得第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
    }
    CGFloat cellX = self.edgeInsets.left + destColumn * (cellW + self.columnMargin);
    CGFloat cellY = minColumnHeight;
    if (cellY != self.edgeInsets.top) {
        cellY += self.rowMargin;
    }
    attrs.frame = CGRectMake(cellX, cellY, cellW, cellH);
    //更新最短那一列的高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    //记录内容的高度 - 即最长那一列的高度
    CGFloat maxColumnHeight = [self.columnHeights[destColumn] doubleValue];
    if (self.contentHeight < maxColumnHeight) {
        self.contentHeight = maxColumnHeight;
    }
    return attrs;
}
 
/**
 * 决定cell的布局属性
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attrsArr;
}
 
/**
 * 内容的高度
 */
- (CGSize)collectionViewContentSize {
    return CGSizeMake(0, self.contentHeight + self.edgeInsets.bottom);
}
@end

总结

难点主要在高度的计算那块,不要算错了,道理都一样,就和collectionViewlayout里面计算高度一样。

========2022.1.15更新==========

上面那种写法会存在这个问题:每次快速上拉获取新数据会导致内存增加,CPU会闪增,再降低,视图显示效果不好

优化

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

推荐阅读更多精彩内容