iOS-CollectionView 瀑布流视图

这里写图片描述
要做瀑布流,首先要清楚几点:

主要是对 UICollectionViewLayout 子类的编写。

第一步、自定义初始化(主要是参数的初始化,行数numberOfLine,行间距rowGap,列间距lineGap,边距sideGap)

第二步、prepareLayout(1.初始化每列高度的字典,2初始化属性数组。)

第三步、layoutAttributesForElementsInRect(返回属性数组。)

第四步、collectionViewContentSize (返回整个 collectionView 的尺寸。)

以下为源代码:

UICollectionViewLayout 子类 WaterLayout

//
//  WaterLayout.h
//  WaterFallLayout2
//
//  Created by zhengbing on 6/30/16.
//  Copyright © 2016 zhengbing. All rights reserved.
//

#import <UIKit/UIKit.h>

//计算高度
typedef CGFloat(^HeightBlock)(NSIndexPath *indexPath, CGFloat width);

@interface WaterLayout : UICollectionViewLayout
@property(nonatomic, assign) NSInteger lineNumber; //每行个数
@property(nonatomic, assign) CGFloat rowGap;    //行距(上下的距离)
@property(nonatomic, assign) CGFloat lineGap;   //列距(中间的距离)
@property(nonatomic, assign) UIEdgeInsets sideGap;  //边距

@property(nonatomic, copy) HeightBlock block;   //计算cell 高度

-(instancetype)initWithLineNumber:(NSInteger)lineNumber rowGap:(CGFloat)rowGap lineGap:(CGFloat)lineGap sideGap:(UIEdgeInsets)sideGap;

@end
//
//  WaterLayout.m
//  WaterFallLayout2
//
//  Created by zhengbing on 6/30/16.
//  Copyright © 2016 zhengbing. All rights reserved.
//

#import "WaterLayout.h"

@interface WaterLayout()
//1.存储每列高度的一个字典
@property(nonatomic, strong) NSMutableDictionary *dictOfLineHeight;
//2.存储所有cell frame 的一个数组
@property(nonatomic, strong) NSMutableArray *arrayOfCellFrame;
@end

@implementation WaterLayout

-(instancetype)initWithLineNumber:(NSInteger)lineNumber rowGap:(CGFloat)rowGap lineGap:(CGFloat)lineGap sideGap:(UIEdgeInsets)sideGap{
    self = [super init];
    if (self) {
        self.lineNumber = lineNumber;
        self.rowGap = rowGap;
        self.lineGap = lineGap;
        self.sideGap = sideGap;
        self.dictOfLineHeight = [NSMutableDictionary dictionary];
        self.arrayOfCellFrame = [NSMutableArray array];
    }
    return self;
}

//自动布局前的准备工作
- (void)prepareLayout{
    //1.要把存储高度的字典补充值
    for (NSInteger i = 0; i < _lineNumber; i++) {
        [self.dictOfLineHeight setObject:@(self.sideGap.top) forKey:[NSString stringWithFormat:@"%ld",i]];
    }
    //2.把cell 的尺寸存进数组
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i=0; i<count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [_arrayOfCellFrame addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
}
//返回所以cell frame 信息的数组
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect{
    return _arrayOfCellFrame;
}
//计算每个cell 的 frame 值
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    //经过了逻辑处理,计算出了cell 的frame
    CGRect frame;
    CGFloat cellW = (self.collectionView.bounds.size.width - _sideGap.left - _sideGap.right - self.lineGap*(self.lineNumber - 1))/self.lineNumber;
    CGFloat cellH;
    if (self.block) {
        cellH = self.block(indexPath,cellW);
    }
    __block NSString *keyOfMinHeight = @"0";
    [_dictOfLineHeight enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([_dictOfLineHeight[keyOfMinHeight] floatValue] > [obj floatValue]) {
            keyOfMinHeight = key;
        }
    }];
    NSInteger minHeiLineNumber = [keyOfMinHeight integerValue];
    CGFloat pointX = (self.sideGap.left + minHeiLineNumber * (self.lineGap + cellW));
    CGFloat pointY = [_dictOfLineHeight[keyOfMinHeight] floatValue];
    frame = CGRectMake(pointX, pointY, cellW, cellH);
    //更新高度
    _dictOfLineHeight[keyOfMinHeight] = @([_dictOfLineHeight[keyOfMinHeight] floatValue] + cellH + self.rowGap);

    attr.frame = frame;
    return attr;
}
//返回 集合视图的宽高
- (CGSize)collectionViewContentSize{
    __block NSString *keyOfMaxHeight = @"0";
    [_dictOfLineHeight enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([_dictOfLineHeight[keyOfMaxHeight] floatValue] < [obj floatValue]) {
            keyOfMaxHeight = key;
        }
    }];
    return CGSizeMake(self.collectionView.bounds.size.width, [_dictOfLineHeight[keyOfMaxHeight] floatValue]);
}

@end

MyCollectionViewCell 用 xib 定义的,里面放有一张添加好约束的图片。

//
//  MyCollectionViewCell.h
//  WaterFallLayout2
//
//  Created by zhengbing on 6/30/16.
//  Copyright © 2016 zhengbing. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic, strong) NSString* imageName;
@end
//
//  MyCollectionViewCell.m
//  WaterFallLayout2
//
//  Created by zhengbing on 6/30/16.
//  Copyright © 2016 zhengbing. All rights reserved.
//

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)setImageName:(NSString *)imageName{
    _imageView.image = [UIImage imageNamed:imageName];
}
@end

DetailsViewController 展示瀑布流视图

//
//  DetailsViewController.m
//  WaterFallLayout2
//
//  Created by zhengbing on 6/30/16.
//  Copyright © 2016 zhengbing. All rights reserved.
//

#import "DetailsViewController.h"
#import "MyCollectionViewCell.h"
#import "WaterLayout.h"

#define CELLID @"DetailsViewController"


@interface DetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic, strong)NSMutableArray * dataSource;
@property(nonatomic, strong)UICollectionView * collectionView;

@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initData];
    [self initUI];
}
-(void)initData{
    _dataSource = [NSMutableArray array];
    for (int i = 0; i < 20; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i+1];
        [_dataSource addObject:imageName];
    }
}
-(void)initUI{
    self.view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark getter and setter
-(UICollectionView *)collectionView{
    if (!_collectionView) {

        //瀑布流布局
        WaterLayout *layout = [[WaterLayout alloc] initWithLineNumber:3 rowGap:10 lineGap:10 sideGap:UIEdgeInsetsMake(10, 10, 10, 10)];
        //block 回调,计算并返回每个 cell 图片的高度
        layout.block = ^CGFloat(NSIndexPath *indexPath, CGFloat width){
            UIImage *image = [UIImage imageNamed:_dataSource[indexPath.item]];
            return (image.size.height/image.size.width)*width;
        };
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor yellowColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CELLID];
    }
    return _collectionView;
}


#pragma mark delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    cell.imageName = _dataSource[indexPath.row];
    return cell;
}
@end
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值