FMLayoutKit
简介
这是一个可以让你更快地完成复杂页面(如电商首页的网格+列表多样式布局)的CollectionView自定义布局,目前支持纵向和横向布局,可以交叉布局,动态cell高度,可以自动计算高度,也可以手动计算通过block返回,并支持长按拖拽排序;通过section和layoutView开关控制是否可拖拽排序,代码可以高度集中,下面有演示,有问题请随时在issue中提问,感谢star。
安装方式
已发布到CocoaPods。在Podfile中添加以下代码:
pod 'FMLayoutKit'
如果Spec的官方源无法拉取,可以使用我的Spec仓库:
https://gitee.com/Coder_FM/FMPodSpec.git
联系方式
周发明, [email protected]
许可证
FMCollectionLayout 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。
提示
在使用动态自动计算高度时,请确保label的preferredMaxLayoutWidth属性给出一个准确值,否则计算label布局的高度可能不准确
插入其他方向的布局时,请使用FMLayoutCrossSection将所需显示的部分包裹起来,可以包裹多个分组布局,该分组也可以添加头部、底部和背景等。
支持的Cell布局样式及头部悬停效果
分组Cell样式1
单一Cell,固定大小,支持多列,从左往右,从上往下布局
分组Cell样式2
单一Cell,固定大小,支持最大行数,从左往右,如果当前屏幕足够大,不会滚动,只有超出部分才会滚动
分组Cell样式3
可以采用多种Cell样式,block返回每个item的大小,从左往右,从上往下寻找最合适的位置放置
分组Cell样式4
瀑布流样式,支持多种cell样式,单列等同于列表样式,列表可变,高度可通过手动计算或通过autolayout布局自动计算(后续配置数据)
分组Cell样式5
标签式布局,支持单种Cell,可单行滚动,也可纵向布局,可限制最大行数(如历史搜索记录那种样式)
分组头部支持的样式有4种
- 一般样式随滚动而滚动
- 悬浮样式随分组滚动
- 始终悬浮,滚动到顶部的样式
- 第一个分组下拉放大效果(效果无法截图)
多屏滑动效果
每个分组都可以设置头部、底部、背景,这三个都有内边距设置,灵活多变
特斯拉滚动视图基于FMLayoutView,共享头部是FMLayoutView,横向每一屏都是FMLayoutView,最底部可以横向滚动的是ScrollView,当触摸头部时,ScrollView的pan手势失效,横向滚动时,将共享头部移至最顶部视图上,滚动结束时静止,将共享头部添加到当前上下滚动的FMLayoutView,以实现效果
使用示例
/// 创建CollectionView delegate以及dataSource默认自己已遵守实现了一些方法
FMLayoutView *view = [[FMLayoutView 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 = [FMLayoutHeader elementSize:100 viewClass:[FMCollectionCustomDecoration class]];
/// 头部最底距离item的间距
section.header.bottomMargin = 10;
/// 头部样式是否悬停
section.header.type = FMHeaderTypeSuspensionBigger;
/// 头部内边距
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 = [FMLayoutFooter elementSize:50 viewClass:[FMCollectionCustomDecoration class]];
section.footer.topMargin = 10;
/// 配置Item样式
section.itemSize = CGSizeMake(100, 100);
section.itemDatas = [@[@"1", @"2", @"3"] mutableCopy];
/// cell的类 可以纯代码也可以Xib
section.cellElement = [FMLayoutElement 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 = @[[FMLayoutElement elementWithViewClass:[FMCollectionCustomCell class]]];
/// 需固定宽度
section.cellFixedSize = [UIScreen mainScreen].bounds.size.width;
/// 手动计算高度
[section setOtherBlock:^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.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;
}
/// 头部共享的集合
- (NSArray<FMLayoutBaseSection *> *)shareSectionsInTesla:(FMTeslaLayoutView *)tesla{
return self.shareSections;
}
版本更新
2020-08-21(1.2.0) 新增单分组隐藏显示 新增动态计算高度计算之后再更改一次的方法
2020-08-21(1.1.9) 修复小bug,增加section刷新方法
2020-08-21(1.1.8) tesla优化修复bug,填充布局支持间距
2020-08-02(1.1.6) 自动计算高度优化
2020-07-24(1.1.3) iOS10,11自动计算高度崩溃的问题修复
2020-07-24(1.1.2) 合并分组优化,创建完之后可以插入分组追加分组
2020-07-21(1.1.1) 合并取消支持拖拽,会出现多重问题,拖拽切换数据的问题修复
2020-07-21(1.1.0) 清除长按拖拽View的背景色 长按拖拽可控制分组中某些item不支持拖拽,修复合并分组的崩溃问题
2020-07-20(1.0.8) 代码优化,新增动态布局不等宽的布局,可实现更多种布局方案
2020-07-16(1.0.7) 优化tesla组件,可以主创建scrollView,但是顶部边距空出来不可用inset操作
2020-07-15(1.0.6) 新增长按拖拽,通过layoutView以及section控制开关。新增纵向穿插横向布局自动滚到中间,并支持自定义根据进度做动画效果
2020-07-14(1.0.5) 修复分组头部底部背景重用标识符问题,代码优化
2020-07-13(1.0.3) 放大缩小效果优化,新增是否可以黏在顶部,新增合并分组,将不同的cell合并到一个分组去
2020-07-09(1.0.2) 新增可复制分组,便于快速创建分组
2020-07-09 重写滚动到某一行的方法,计算出偏移量
2020-07-08(1.0.1) 由老版本FMCollectionLayout优化更改名字为FMLayoutKit重新上传
感谢观看
有使用问题,欢迎联系我随时交流,QQ:847881570,能Star一下的话,感激不尽