UICollectionViewLayout详解,文档翻译

实现一个UICollectionView,和UITableView类似,不过初始化的时候要传入一个UICollectionViewLayout。

  • 苹果给UIcollectionview中的所有视图都来自一个可重用的基类,就是UICollectionReusableView。意思就是UIcollectionViewCell和自定义SupplementaryView和DecorationView都是继承者这个UICollectionReusableView类来构建自己的视图。

  • UICollectionViewLayout是UICollectionView最精髓的部分,主要用来为collection view的元素提供位置信息和状态信息。UICollectionViewLayout并不负责创建视图,这些视图是collection view的数据源(data source)创建的。布局对象只不过定义了这些视图的位置和大小。有了它我们可以自定义Cell的布局,苹果为我们设计了一套非常灵活通用的Layout子类UICollectionViewFlowLayout。许多大神在博客上已经对其进行了详细讲解,本篇主要是记录我对UICollectionViewFlowLayout的理解。

  • 需要注意的是,collection view进行的布局限制在屏幕可见的范围之内。(注:即,即便创建了所有元素的布局对象,但是真正的布局只是在可见的范围内,超出屏幕的部分没有布局

UICollectionViewLayoutAttributes 布局属性

该属性可以拿到每个Cell、SupplementaryView、DecorationView的布局属性,反过来,collection view 使用该属性将cell和SupplementaryView放在其边界内。

CGRect frame 布局视图的frame简单明了
CGPoint center 视图中心点
CGSize size 视图尺寸
CATransform3D transform3D 这个属性可以用来做酷炫的3D动画
CGAffineTransform transform 转场属性
CGFloat alpha 透明度
NSInteger zIndex 层级,数字越大,层级越高(最上面)。
NSIndexPath *indexPath 如果是cell有对应的indexPath
UICollectionElementCategory representedElementCategory 视图标记,是cell还是supplementary View或者decoration View
registerClass:forDecorationViewOfKind: 注册decoration View
registerNib:forDecorationViewOfKind:
+(instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath 这个类方法是decoration View布局的来源
-(nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath 与上一个方法结合得到decoration View布局

UICollectionViewLayoutInvalidationContext

当UICollectionView调用invalidateLayoutWithContext:方法重新布局的时候配置该参数,指定哪些cell需要重新布局

UICollectionViewLayout

该类指定UICollectionView中每个元素的布局
UICollectionViewLayout有3个分类,它们的大概作用如下:

  • @interface UICollectionViewLayout (UISubclassingHooks) 这个扩展类中,都是用来布局UIcollectionView子视图的
  • @interface UICollectionViewLayout (UIUpdateSupportHooks) 用来布局删除插入动作
  • @interface UICollectionViewLayout (UIReorderingSupportHooks) 移动动作布局

1、UICollectionViewLayout (UISubclassingHooks)这个分类主要做布局,在这个分类里需要重写的方法 :


// 1、准备布局,布局初始化一般在这里进行
- (void)prepareLayout;

// 每当collectionView边界改变时便调用这个方法询问 是否 重新初始化布局 是则调用prepareLayout准备布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;

// 2、初始化布局时调用,返回特定rect(有可能是也可能不是可见rect),每个cell和Supplementary和Decoration的布局属性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect: (CGRect)rect;

// 当滚动停止时,会调用该方法确定collectionView滚动到的位置
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity;
//在使用UICollectionViewFlowLayout布局的时候,有时会有特别的需求,比如:
//当一个cell滑动到屏幕中点的时候放大,并且当我停止滑动时,能够将离屏幕最近的cell居中。
//这四个方法就能轻松的完成这样的事。





- (CGSize)collectionViewContentSize;

返回collectionView内容区的宽度和高度,子类必须重载该方法,
返回值代表了所有内容的宽度和高度,而不仅仅是可见范围的,
collectionView通过该信息配置它的滚动范围,默认返回 CGSizeZero。


- (NSArray<__kindofUICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
返回UICollectionViewLayoutAttributes 类型的数组,
UICollectionViewLayoutAttributes 对象包含cell或view的布局信息。
子类必须重载该方法,并返回该区域内所有元素的布局信息,包括cell,追加视图和装饰视图。

在创建UICollectionViewLayoutAttributes的时候,创建的是相应元素类型(cell, supplementary, decoration)的 attributes对象,比如:
+ (instancetype)layoutAttributesForCellWithIndexPath:(NSIndexPath *)indexPath;
+ (instancetype)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind withIndexPath:(NSIndexPath *)indexPath;
+ (instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath;
UICollectionView 根据不同的类型区分属性,并根据这些信息决定创建怎样的视图及如何进行管理。


-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
返回指定indexPath的item的布局信息。子类必须重载该方法,该方法只能为cell提供布局信息,不能为补充视图和装饰视图提供。

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
如果你的布局支持追加视图的话,必须重载该方法,该方法返回的是追加视图的布局信息,
kind这个参数区分段头还是段尾的,在collectionview注册的时候回用到该参数。

- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath;
如果你的布局支持装饰视图的话,必须重载该方法,该方法返回的是装饰视图的布局信息,
ecorationViewKind这个参数在collectionview注册的时候回用到

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
该方法用来决定是否需要更新布局。如果collection view需要重新布局返回YES,否则返回NO,默认返回值为NO。
子类重载该方法的时候,基于是否collectionview的bounds的改变会引发cell和view布局的改变(滚动屏幕cell或者view会不会变),给出正确的返回值。
如果返回YES,UICollectionView通过调用invalidateLayoutWithContext方法使原来的layout失效

这些方法为collection view 在屏幕上布局提供了最基础的布局信息,如果你不想为追加视图和装饰视图布局,可以不去重载相应的方法。

2、UICollectionViewLayout (UIUpdateSupportHooks)这个分类主要做删除插入等动作,

  • 当collection view的数据发生改变的时候,比如插入或者删除 item的时候,collection view将会要求布局对象更新相应的布局信息。添加、删除 items时都必须更新相应的布局信息以便反映元素最新的位置。
  • 而collection view删除或者添加元素的时候,将会调用一些不同的方法,你应该重载以便提供正确的布局信息.
    在这个分类里需要重写的方法 :

- (void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems;
做一些和布局相关的准备工作。该方法在插入删除动作之前被调用



  /**  一般来说,我们对布局属性从初始状态到结束状态进行线性插值来计算 collection view 的动画参数。
    然而,新插入或者删除的元素并没有最初或最终状态来进行插值。
    要计算这样的 cells 的动画,collection view 将通过 initialLayoutAttributesForAppearingItemAtIndexPath:
     以及 finalLayoutAttributesForAppearingItemAtIndexPath: 方法来询问其布局对象,
    以获取最初的和最后的属性。
    下面这两个方法是成对出现的,一个是在在屏幕上出现之前调用,一个是在屏幕上出现之后调用
    在[collecView reloadData]或者performBatchUpdates:completion:调用的时候
    只要是有刷新的效果,他们就会被调用多次,视图不断的消失(失效,被摧毁)出现(重组 被创建或者回收利用)*/

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//在一个 item被插入到collection view 的时候,返回开始的布局信息。
//这个方法在 prepareForCollectionViewUpdates:之后和finalizeCollectionViewUpdates 之前调用。
//UICollectionView将会使用该布局信息作为动画的起点(结束点是该item在UICollectionView的最新的位置)。
//如果返回为nil,布局对象将用item的最终的attributes 作为动画的起点和终点。

- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//返回值是item即将从collection view移除时候的布局信息,对即将删除的item来讲,
//该方法在 prepareForCollectionViewUpdates: 之后和finalizeCollectionViewUpdates 之前调用。
//在该方法中返回的布局信息描包含 item的状态信息和位置信息。
//UICollectionView将会把该信息作为动画的终点(起点是item当前的位置)。
//如果返回为nil的话,布局对象将会把当前的attribute,作为动画的起点和终点。

也可以重载
- (void)finalizeCollectionViewUpdates;
通过该方法添加一些动画到block,或者做一些和最终布局相关的工作。更新结束后调用

//返回值是追加视图插入UICollectionView时的布局信息。以下方法使用同initialLayoutAttributesForAppearingItemAtIndexPath:
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
//返回值是装饰视图插入UICollectionView时的布局信息。
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
//返回值是追加视图删除UICollectionView时的布局信息。
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
//返回值是装饰视图删除UICollectionView时的布局信息。
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath




举例:demo下载(https://github.com/mpospese/CircleLayout)

demo中当调用
[self.collectionView performBatchUpdates:^{
  [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:tappedCellPath]];  
  } completion:nil];
方法删除cell的时候,在UICollectionViewLayout中,以下方法会被先后调用:
1、- (void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems;
2、- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
3、- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
4、- (void)finalizeCollectionViewUpdates
其中2、3方法会反复调用多次。视图不断的消失(失效,被摧毁)出现(重组 被创建或者回收利用)

** 3、UICollectionViewLayout (UIReorderingSupportHooks) 移动动作布局**

  • 对于移动的元素,UICollectionView提供了标准的方法获取更新后的布局信息。
- (NSIndexPath *)targetIndexPathForInteractivelyMovingItem:(NSIndexPath *)previousIndexPath withPosition:(CGPoint)position NS_AVAILABLE_IOS(9_0);
根据item在UICollectionView中的位置获取该item的NSIndexPath。
第一个参数该item原来的index path,第二个参数是item在collection view中的位置。
 在item移动的过程中,该方法将UICollectionView中的location映射成相应 NSIndexPaths。该方法的默认是显示,查找指定位置的已经存在的cell,返回该cell的NSIndexPaths 。如果在相同的位置有多个cell,该方法默认返回最上层的cell。


- (UICollectionViewLayoutAttributes *)layoutAttributesForInteractivelyMovingItemAtIndexPath:(NSIndexPath *)indexPath withTargetPosition:(CGPoint)position NS_AVAILABLE_IOS(9_0);
当item在手势交互下移动时,通过该方法返回这个item布局的attributes 。
默认实现是,复制已存在的attributes,改变attributes两个值,一个是中心点center;另一个是z轴的坐标值,设置成最大值。
所以该item在collection view的最上层。子类重载该方法,可以按照自己的需求更改attributes,
首先需要调用super类获取attributes,然后自定义返回的数据结构。

- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray<NSIndexPath *> *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray<NSIndexPath *> *)previousIndexPaths previousPosition:(CGPoint)previousPosition NS_AVAILABLE_IOS(9_0);
- (UICollectionViewLayoutInvalidationContext *)invalidationContextForEndingInteractiveMovementOfItemsToFinalIndexPaths:(NSArray<NSIndexPath *> *)indexPaths previousIndexPaths:(NSArray<NSIndexPath *> *)previousIndexPaths movementCancelled:(BOOL)movementCancelled NS_AVAILABLE_IOS(9_0);

UICollectionViewFlowLayout流水布局

它继承自UICollectionViewLayout。


属性.png

基本都是一看就知道它是干嘛的。
但是如果每个cell布局样式都不一样的话,就要在代理方法里面设置这些属性

通过代理设置.png

也是顾名思义的。在子类化UICollectionViewLayout之前,应该先了解UICollectionViewFlowLayout,看看它是否已经能够满足你的布局需求。

参考:http://blog.csdn.net/qq_30513483/article/details/51611653

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容