
本實作的特點是可自由設定瀑布流的總列數(示例為 2 列)。雖然 iPhone 系統相簿未使用這種布局,但瀑布流仍是一種非常常見的排版方式。
我們需要自訂 UICollectionViewCell 與 UICollectionViewLayout:
1、自訂 UICollectionViewCell,內部只需一個佔滿整個 cell 的 UIImageView。
2、重點是自訂 UICollectionViewLayout——務必繼承自 UICollectionViewLayout,千萬別繼承 UICollectionViewFlowLayout。
3、此外還需計算圖片高度,讓圖片依原始比例等比例縮放顯示,避免變形,同時也用它來計算整個 CollectionView 的 contentSize。
// 1. 計算每個 item 的大小和位置 - (void)prepareLayout; // 2. 返回每個 item 的布局屬性 - (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; // 3. 返回 collectionView 的總高度 - (CGSize)collectionViewContentSize;
第二個方法需回傳一個存放 UICollectionViewLayoutAttributes 的陣列,因此要準備一個屬性陣列 attributesArray 來保存並回傳。此外還需要 numberOfColumns(列數,因要在外部設定故寫在 .h)、itemWidth(單項寬度)與 contentHeight(內容總高度)等屬性。
- (instancetype)init {
self = [super init];
if (self) {
_attributesArray = [NSMutableArray array];
_numberOfColumns = 2; // 預設 2 列
_contentHeight = 0.0f;
_cellMargin = 5.0f; // 間距
}
return self;
}
- (CGFloat)itemWidth {
// 所有邊距之和:兩列有三個邊距,三列有四個邊距
CGFloat allMargin = (_numberOfColumns + 1) * _cellMargin;
CGFloat noMarginWidth = CGRectGetWidth(self.collectionView.bounds) - allMargin;
return noMarginWidth / _numberOfColumns;
}
此方法是核心,需記錄「目前最矮的列」,並把每個 item 依序放到最矮的列上,同時更新各列累積高度與整體內容高度:
- (void)prepareLayout {
NSInteger shortestColumn = 0; // 記錄最矮的列(從 0 開始)
NSMutableArray *columnHeightArray = [NSMutableArray array];
for (int i = 0; i < _numberOfColumns; i++) {
[columnHeightArray addObject:@(_cellMargin)]; // 各列初始高度設為間距
}
for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
[_attributesArray addObject:layoutAttributes];
CGFloat x = (self.itemWidth + _cellMargin) * shortestColumn + _cellMargin;
CGFloat y = [columnHeightArray[shortestColumn] floatValue];
CGFloat width = self.itemWidth;
// 透過代理取得該 item 的高度
CGFloat height = [self.delegate collectionView:self.collectionView layout:self width:self.itemWidth heightForItemAtIndexPath:indexPath];
layoutAttributes.frame = CGRectMake(x, y, width, height);
// 更新該列的累積高度
columnHeightArray[shortestColumn] = @([columnHeightArray[shortestColumn] floatValue] + height + _cellMargin);
self.contentHeight = MAX(self.contentHeight, [columnHeightArray[shortestColumn] floatValue]);
// 找出新的最矮列
for (int i = 0; i < _numberOfColumns; i++) {
CGFloat currentHeight = [columnHeightArray[shortestColumn] floatValue];
CGFloat height = [columnHeightArray[i] floatValue];
if (currentHeight > height) {
shortestColumn = i;
}
}
}
}
// 返回每個 item 對應的布局屬性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
return _attributesArray;
}
// 返回 CollectionView 的滾動範圍
- (CGSize)collectionViewContentSize {
return CGSizeMake(0, _contentHeight);
}
CustomCollectionViewLayout *layout = [[CustomCollectionViewLayout alloc] init]; layout.numberOfColumns = 2; // 設定列數,2 或 3 列為佳 layout.delegate = self; // 指定 VC 為計算高度協議的代理
代理方法則依傳入的寬度,用 AVMakeRectWithAspectRatioInsideRect 計算等比縮放後的高度(需引入 AVFoundation):
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout width:(CGFloat)width heightForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
UIImage *image = _imagesArray[indexPath.row];
CGRect boundingRect = CGRectMake(0, 0, width, CGFLOAT_MAX);
CGRect imageCurrentRect = AVMakeRectWithAspectRatioInsideRect(image.size, boundingRect);
return imageCurrentRect.size.height;
}
以上就是 iOS 瀑布流效果的完整實作。關鍵在於自訂 UICollectionViewLayout 並在 prepareLayout 中「貪婪地」把每個項目放到目前最矮的列,再依比例計算高度,即可實現整齊美觀的瀑布流排版,希望對你的開發有所幫助。