FMCollectionLayout 1.2.2

FMCollectionLayout 1.2.2

zfm8721209 维护。



  • 周发明

当前版本不再进行重大更新,可以使用功能更全的 FMLayoutKit

FMCollectionLayout

CI Status Version License Platform

简介

一个可以让你更快的搞定复杂页面(电商首页,方格+列表多样式布局)的CollectionView自定义布局,目前仅支持纵向布局,可以穿插横向布局,动态cell高度做一些适配可以做到自动计算高度,也可以手动计算通过block返回,代码可以高度集中在一块,效果下面有演示,有问题随时issue我,感谢Star

安装方式

已发布到CocoaPods。在 podfile 中添加以下代码:

pod 'FMCollectionLayout'

如果无法拉取 Spec 的官方源,可以使用我自己的一个 Spec 仓库:

https://gitee.com/Coder_FM/FMPodSpec.git

联系方式

周发明,[email protected]

授权

FMCollectionLayout遵从MIT授权协议。详细信息请参阅LICENSE文件。

提示

在使用动态自动计算高度时 label的preferredMaxLayoutWidth属性请提供一个准确的值 否则label布局的高度计算将不准确

支持的Cell布局样式以及头部悬停效果

效果图片

分组Cell样式1

单一Cell,固定大小,支持多列,从左到右,从上到下布局

分组Cell样式2

单一Cell,固定大小,支持最大行数,从左到右,如果当前屏幕足够宽,不会滚动,多余部分才会滚动

效果图片

分组Cell样式3

支持多种Cell样式,block返回每一个item的大小,从左往右,从上往下寻找最合适的位置放置

效果图片 效果图片

分组Cell样式4

瀑布流样式,支持多种cell样式,单列即为列表样式,列表可变,高度可以通过手动计算或通过autolayout自动计算(续配置数据)

效果图片

分组Cell样式5

标签式布局,支持单种cell,可支持单行滚动,也可支持纵向布局,可限制最大行数(如同历史搜索记录的样式)

分组头部支持的样式有4种

  • 一般样式跟随滚动
  • 悬浮随分组滚动
  • 始终悬浮,滚动至顶部样式
  • 第一个分组下拉放大效果(效果无法截图)

多屏滑动效果

效果图片 效果图片

每个分组均可设置头部、底部及背景,这三个都支持内边距设置(inset),具有很高的灵活性

特斯拉滚动视图基于FMCollectionLayoutView,共享头部是FMCollectionLayoutView,每一屏横向也是FMCollectionLayoutView,最底部支持横向滚动的是一个ScrollView,当触摸头部时,ScrollView的pan手势失效,横向滚动时,共享头部移至最顶部视图,滚动结束后静止时,将共享头部添加到当前上下滚动的FMCollectionLayoutView中,以达到效果

使用示例

/// 创建CollectionView  delegate以及dataSource默认自己已遵守实现了一些方法
FMCollectionLayoutView *view = [[FMCollectionLayoutView alloc] init];
/// 需要布局的分组
[view.layout setSections:self.shareSections];
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.bottom.mas_equalTo(0);
    make.top.mas_equalTo(100);
}];
self.collectionView = view;

/// 固定大小 单一cell样式的分组
 FMLayoutFixedSection *section = [FMLayoutFixedSection sectionWithSectionInset:UIEdgeInsetsMake(0, 15, 15, 15) itemSpace:10 lineSpace:10 column:2];
/// 配置分组头部  高度以及view类
section.header = [FMSupplementaryHeader supplementaryHeight:100 viewClass:[FMCollectionCustomDecoration class]];
/// 头部最底距离item的间距
section.header.bottomMargin = 10;
/// 头部样式是否悬停
section.header.type = FMSupplementaryTypeSuspensionBigger;
/// 头部内边距
section.header.inset = UIEdgeInsetsMake(0, -15, 0, -15);
/// 
[section setConfigureHeaderData:^(FMLayoutBaseSection * _Nonnull section, UICollectionReusableView * _Nonnull header) {
    FMCollectionCustomDecoration *customHeader = (FMCollectionCustomDecoration *)header;
    customHeader.textLabel.text = @"固定大小, 从左往右从上往下排的分组, 头部放大缩放效果";
}];
/// 配置分组底部
section.footer = [FMSupplementaryFooter supplementaryHeight:50 viewClass:[FMCollectionCustomDecoration class]];
section.footer.topMargin = 10;

/// 配置Item样式
/// 是否可以横向滚动
section.isHorizontalCanScroll = YES;
section.itemSize = CGSizeMake(100, 100);
section.itemDatas = [@[@"1", @"2", @"3"] mutableCopy];
/// cell的类 可以纯代码也可以Xib
section.cellElement = [FMCollectionViewElement elementWithViewClass:[FMCollectionCustomCell class]];
/// cell赋值数据
[section setConfigureCellData:^(FMLayoutBaseSection * _Nonnull section, UICollectionViewCell * _Nonnull cell, NSInteger item) {
    
}];
/// cell点击事件
[section setClickCellBlock:^(FMLayoutBaseSection * _Nonnull section, NSInteger item) {
    FMAddViewController *add = [[FMAddViewController alloc] init];
    [self.navigationController pushViewController:add animated:YES];
}];

#pragma mark --- 动态分组
/// cell类的数组
section.cellElements = @[[FMCollectionViewElement elementWithViewClass:[FMCollectionCustomCell class]]];
/// 需固定宽度
section.cellFixedWidth = [UIScreen mainScreen].bounds.size.width;
/// 手动计算高度
[section setHeightBlock:^CGFloat(id  _Nonnull section, NSInteger item) {
    return 100 + item * 100;
}];
/// 或者可以自动计算高度  布局约束好  数据填充完
section.autoHeightFixedWidth = YES;

/// 对应index需要返回的reusedId来取对应的cell
[section setDeqCellReturnReuseId:^NSString * _Nonnull(FMLayoutDynamicSection * _Nonnull section, NSInteger index) {
    return [section.cellElements firstObject].reuseIdentifier;
}];

#pragma mark --- 标签分组
/// 是否是单行滚动
section.isSingleLineCanScroll = YES;
/// 不是单行的话  可以限制最大行数
section.maxLine = 6;
/// 固定每一个高度
section.cellFixedHeight = 40;
/// 返回对应的宽度
[section setWidthBlock:^CGFloat(id  _Nonnull section, NSInteger item) {
    return item * 20 + 100;
}];

#pragma mark --- 填充布局分组
/// 需返回大小  插入到合适的位置
[section setSizeBlock:^CGSize(id  _Nonnull section, NSInteger item) {
    switch (item) {
        case 2:
            return CGSizeMake(150, 140.32);
        case 5:
            return CGSizeMake((self.view.frame.size.width-20-150)/2, 70.19);
        case 8:
        case 11:
            return CGSizeMake(100, 240);
        case 10:
            return CGSizeMake(self.view.frame.size.width-20-200, 140);
        case 9:
        case 12:
            return CGSizeMake(self.view.frame.size.width-20-100, 100);
        case 0:
        case 1:
        case 3:
        case 4:
            return CGSizeMake((self.view.frame.size.width-20-150)/4, 70.13);
        default:
            return CGSizeMake((self.view.frame.size.width-20-150)/4, 70.19);
    }
}];

#pragma mark --- 特斯拉组件的使用说明  内部都是FMCollectionLayoutView的组合
/// 创建组件 遵守代理并实现必须的方法
FMTeslaLayoutView *multi = [[FMTeslaLayoutView alloc] init];
multi.delegate = self;
multi.dataSource = self;
[self.view addSubview:multi];
/// 悬停头部的最小高度 伸缩动画用
- (CGFloat)shareSuspensionMinHeightWithTesla:(FMTeslaLayoutView *)tesla{
    return 70;
}
/// 即将创建FMCollectionLayoutView  每一个分页只创建一个  懒加载
- (void)tesla:(FMTeslaLayoutView *)tesla willCreateLayoutViewWithIndex:(NSInteger)index{
    NSLog(@"willCreateLayoutViewWithIndex %ld", (long)index);
}
// 已创建FMCollectionLayoutView
- (void)tesla:(FMTeslaLayoutView *)tesla didCreatedLayoutViewWithIndex:(NSInteger)index layoutView:(FMCollectionLayoutView *)layoutView{
    NSLog(@"didCreatedLayoutViewWithIndex %ld", (long)index);
}
// 分页滚动到哪一页  并返回当前页的layoutView
- (void)tesla:(FMTeslaLayoutView *)tesla didScrollEnd:(NSInteger)index currentLayoutView:(nonnull FMCollectionLayoutView *)layoutView{
    [self.navTitleView selectWithIndex:index];
}
/// 分页个数
- (NSInteger)numberOfScreenInTesla:(nonnull FMTeslaLayoutView *)tesla {
    return 4;
}
/// 对应页面的view需要展示的Sections集合
- (nonnull NSMutableArray<FMLayoutBaseSection *> *)tesla:(nonnull FMTeslaLayoutView *)tesla sectionsInScreenIndex:(NSInteger)screenIndex {
    return [self.sections mutableCopy];
}
/// 头部共享的集合
- (NSArray<FMLayoutBaseSection *> *)shareSectionsInTesla:(FMTeslaLayoutView *)tesla{
    return self.shareSections;
}

如有使用问题,欢迎随时联系我交流,QQ:847881570,如您能Star一下,将不胜感激