dsd# FMMosaicLayout
FMMosaicLayout 是一个马赛克集合视图布局。有很多基于媒体的 iOS 应用程序,没有任何修改就使用了 UICollectionViewFlowLayout
。这使得展示显得单调,并且没有吸引力。FMMosaicLayout 正是在正确的方向上迈出的一步。只需将此存入您的项目中,设置您的首选项,它就会以漂亮的马赛克布局您的集合视图单元格。这个算法的灵感来源于这篇 博客文章。
FMMosaicLayout 非常容易使用。以下就是开始所需的所有内容。
- (void)viewDidLoad {
...
FMMosaicLayout *mosaicLayout = [[FMMosaicLayout alloc] init];
self.collectionView.collectionViewLayout = mosaicLayout;
...
}
#pragma mark <FMMosaicLayoutDelegate>
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(FMMosaicLayout *)collectionViewLayout
numberOfColumnsInSection:(NSInteger)section {
return 2; // Or any number of your choosing.
}
您还可以通过 Interface Builder 设置布局。要查看完整的示例实现,请克隆仓库,并在示例目录下运行 pod install
。然后打开 Example/FMMosaicLayout.xcworkspace
。
除了必需的协议方法 collectionView:layout:numberOfColumnsInSection:
之外,您还可以根据需要实现来自 FMMosaicLayoutDelegate
的几个可选方法。您可以在示例项目中看到它们的使用。
- (FMMosaicCellSize)collectionView:(UICollectionView *)collectionView layout:(FMMosaicLayout *)collectionViewLayout
mosaicCellSizeForItemAtIndexPath:(NSIndexPath *)indexPath;
这允许您指定何时放置大或小的马赛克单元格。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(FMMosaicLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;
在此处,您可以针对每个分区指定一个自定义的 UIEdgeInsets
。
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(FMMosaicLayout *)collectionViewLayout
interitemSpacingForSectionAtIndex:(NSInteger)section;
在此处,您可以指定单元格之间的间距。
以下是一些您可以使用来自定义标题和页脚的可选方法。首先是设置标题和页脚高度的请求。
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(FMMosaicLayout *)collectionViewLayout
heightForHeaderInSection:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(FMMosaicLayout *)collectionViewLayout
heightForFooterInSection:(NSInteger)section;
如果您的标题和/或页脚想要覆盖马赛克单元格,请在您的代理中实现以下方法并让它们返回 YES
。默认值是 NO
。
- (BOOL)headerShouldOverlayContentInCollectionView:(UICollectionView *)collectionView
layout:(FMMosaicLayout *)collectionViewLayout;
- (BOOL)footerShouldOverlayContentInCollectionView:(UICollectionView *)collectionView
layout:(FMMosaicLayout *)collectionViewLayout;
FMMosaicLayout 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。