首页 > 代码库 > UICollectionView瀑布流的实现
UICollectionView瀑布流的实现
实现瀑布流的效果我们可以通过重写 UICollectionViewLayout里面
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #ba2da2 }</style>- (void)prepareLayout;
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #703daa }</style>
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #703daa }</style>
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #703daa }</style>
- (CGSize)collectionViewContentSize;
的这4个方法。。。
下面是4个方法的具体实现
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #3e1e81 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000; min-height: 15.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4f8187 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "PingFang SC"; color: #008400 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #703daa } span.s1 { } span.s2 { font: 13.0px "PingFang SC" } span.s3 { color: #ba2da2 } span.s4 { color: #000000 } span.s5 { color: #272ad8 } span.s6 { font: 13.0px Menlo; color: #000000 } span.s7 { font: 13.0px Menlo } span.s8 { color: #4f8187 } span.s9 { color: #703daa } span.s10 { color: #31595d } span.s11 { color: #3e1e81 } span.s12 { color: #008400 } span.s13 { font: 13.0px "PingFang SC"; color: #008400 }</style>/**
* 初始化
*/
- (void)prepareLayout
{
[super prepareLayout];
self.contentHeight = 0;
// 清除以前计算的所有高度
[self.columnHeights removeAllObjects];
for (NSInteger i = 0; i < self.columnCount; i++) {
[self.columnHeights addObject:@(self.edgeInsets.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];
}
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #ba2da2 } span.s1 { } span.s2 { font: 13.0px "PingFang SC" } span.s3 { color: #703daa } span.s4 { color: #000000 } span.s5 { color: #4f8187 }</style>
/**
* 决定cell的排布
*/
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attrsArray;
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #703daa } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000; min-height: 15.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #31595d } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "PingFang SC"; color: #008400 } span.s1 { } span.s2 { font: 13.0px "PingFang SC" } span.s3 { color: #703daa } span.s4 { color: #000000 } span.s5 { color: #3e1e81 } span.s6 { color: #ba2da2 } span.s7 { color: #31595d } span.s8 { color: #272ad8 } span.s9 { color: #4f8187 } span.s10 { font: 13.0px Menlo; color: #000000 } span.s11 { font: 13.0px Menlo }</style>
/**
* 返回indexPath位置cell对应的布局属性
*/
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 创建布局属性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
// collectionView的宽度
CGFloat collectionViewW = self.collectionView.frame.size.width;
// 设置布局属性的frame
CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
CGFloat h = [self.delegate waterflowLayout:self heightForItemAtIndex:indexPath.item itemWidth:w];
// 找出高度最短的那一列
NSInteger destColumn = 0;
CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 1; i < self.columnCount; i++) {
// 取得第i列的高度
CGFloat columnHeight = [self.columnHeights[i] doubleValue];
if (minColumnHeight > columnHeight) {
minColumnHeight = columnHeight;
destColumn = i;
}
}
CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
CGFloat y = minColumnHeight;
if (y != self.edgeInsets.top) {
y += self.rowMargin;
}
attrs.frame = CGRectMake(x, y, w, h);
// 更新最短那列的高度
self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
// 记录内容的高度
CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
if (self.contentHeight < columnHeight) {
self.contentHeight = columnHeight;
}
return attrs;
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #703daa } span.s3 { color: #ba2da2 } span.s4 { color: #3e1e81 } span.s5 { color: #272ad8 } span.s6 { color: #4f8187 } span.s7 { color: #31595d }</style>
- (CGSize)collectionViewContentSize
{
return CGSizeMake(0, self.contentHeight + self.edgeInsets.bottom);
}
效果如下图:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #703daa } span.s1 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #3e1e81 } span.s1 { color: #000000 } span.s2 { color: #ba2da2 } span.s3 { }</style>
UICollectionView瀑布流的实现