这是一个由 Facebook 照片部分启发的 UICollectionView 布局。这个布局加载了方形的项目和随机大小的完整尺寸项目。它使用标准布局代理方法。不需要实现额外的自定义方法。
您可以通过在 podfile 中添加此布局来在项目中使用它
pod 'FBLikeLayout'
只需要分配一个新的 FBLikeLayout
,然后自定义该布局附带的一些属性
FBLikeLayout *layout = [FBLikeLayout new];
//in this case we want 3 cells per row, maximum. This is also the default value if you do not customize the layout.singleCellWidth property
CGFLoat cellWidth = (MIN(self.collectionView.bounds.size.width, self.collectionView.bounds.size.height)-self.collectionView.contentInset.left-self.collectionView.contentInset.right-8)/3.0;
layout.minimumInteritemSpacing = 4;
layout.singleCellWidth = cellWidth;
layout.maxCellSpace = 3; //for full size cells, this parameter determines the max cell space
//if you want the items size to be forced in order to have the minimumInteritemSpacing always respected. Otherwise the interitem spacing will be adapted in order to cover the complete row with cells
layout.forceCellWidthForMinimumInteritemSpacing = YES;
layout.fullImagePercentageOfOccurrency = 25; //this percent value determines how many times randomly the item will be full size.
self.collectionView.collectionViewLayout = layout;
然后只需实现 UICollectionViewDelegateFlowLayout 方法即可
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath