iOS之瀑布流布局设计
2016-04-03 14:50
435 查看
#import "CXDWaterflowLayout.h" /** 默认的列数 */ static const NSInteger CXDDefaultColumnCount = 3; /** 每一列之间的间距 */ static const CGFloat CXDDefaultColumnMargin = 10; /** 每一行之间的间距 */ static const CGFloat CXDDefaultRowMargin = 10; /** 边缘间距 */ static const UIEdgeInsets CXDDefaultEdgeInsets = {10, 10, 10, 10}; @interface CXDWaterflowLayout() /** 存放所有cell的布局属性 */ @property (nonatomic, strong) NSMutableArray *attrsArray; /** 存放所有列的当前高度 */ @property (nonatomic, strong) NSMutableArray *columnHeights; @end @implementation CXDWaterflowLayout - (NSMutableArray *)columnHeights { if (!_columnHeights) { _columnHeights = [NSMutableArray array]; } return _columnHeights; } - (NSMutableArray *)attrsArray { if (!_attrsArray) { _attrsArray = [NSMutableArray array]; } return _attrsArray; } /** * 初始化 */ - (void)prepareLayout { [super prepareLayout]; // 清除以前计算的所有高度 [self.columnHeights removeAllObjects]; for (NSInteger i = 0; i < CXDDefaultColumnCount; i++) { [self.columnHeights addObject:@(CXDDefaultEdgeInsets.top)]; } // 清除之前所有的布局属性 [self.attrsArray removeAllObjects]; // 开始创建每一个cell对应的布局属性 NSInteger count = [self.collectionView numberOfItemsInSection:0]; for (NSInteger i = 0; i < count; i++) { // 创建位置 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; // 获取indexPath位置cell对应的布局属性 UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath]; [self.attrsArray addObject:attrs]; } } /** * 决定cell的排布 */ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { return self.attrsArray; } /** * 返回indexPath位置cell对应的布局属性 */ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { // 创建布局属性 UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; // collectionView的宽度 CGFloat collectionViewW = self.collectionView.frame.size.width; // 设置布局属性的frame CGFloat w = (collectionViewW - CXDDefaultEdgeInsets.left - CXDDefaultEdgeInsets.right - (CXDDefaultColumnCount - 1) * CXDDefaultColumnMargin) / CXDDefaultColumnCount; CGFloat h = 50 + arc4random_uniform(100); // 找出高度最短的那一列 NSInteger destColumn = 0; CGFloat minColumnHeight = [self.columnHeights[0] doubleValue]; for (NSInteger i = 1; i < CXDDefaultColumnCount; i++) { // 取得第i列的高度 CGFloat columnHeight = [self.columnHeights[i] doubleValue]; if (minColumnHeight > columnHeight) { minColumnHeight = columnHeight; destColumn = i; } } CGFloat x = CXDDefaultEdgeInsets.left + destColumn * (w + CXDDefaultColumnMargin); CGFloat y = minColumnHeight; if (y != CXDDefaultEdgeInsets.top) { y += CXDDefaultRowMargin; } attrs.frame = CGRectMake(x, y, w, h); // 更新最短那列的高度 self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame)); return attrs; } - (CGSize)collectionViewContentSize { CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue]; for (NSInteger i = 1; i < CXDDefaultColumnCount; i++) { // 取得第i列的高度 CGFloat columnHeight = [self.columnHeights[i] doubleValue]; if (maxColumnHeight < columnHeight) { maxColumnHeight = columnHeight; } } return CGSizeMake(0, maxColumnHeight + CXDDefaultEdgeInsets.bottom); } @end
ps:继承UICollectionViewLayout类,实现其中方法,找到高度最短的那一列填充对应数据即可
相关文章推荐
- wordpress 移动客户端(安卓,ios)
- iOS开发中ViewController的生命周期分析和使用方法
- IOS --- CocoaPods的安装和使用
- 解读为毛我的iPhone5老是升级不了iOS9.3新系统
- IOS 手势详解
- iOS本地存储2--CoreData存储数据
- IOS成长之路-使用xib界面与和代码相关联的方法
- 几个有关iOS的几个常见问题-----RunTime
- IOS开发新的一天
- iOS 保存多张bundle里面的图片到相册
- iOS报错--Init methods must return a type related to the receiver type
- iOS——plist对象转模型
- iOS——自定义cell的两种方式
- iOS开发- 隐藏键盘总结
- ios项目提交审核过程
- iOS开发 -- 发送JSON数据给服务器
- ios扫码应用总结
- iOS开发 ☞ NSString详解
- iOS_后缀为gif的图片显示
- 【转载】iostat和iowait详细解说