Supplementary Views 追加视图 (相似Header或者Footer)
Decoration Views 装饰视图 (用做背景展现)
复制代码
对于cell的样式和组织方式,因为collectionView比tableView要复杂得多,所以没有按照相似于tableView的style的方式来定义,而是专门使用了一个类来对collectionView的布局和行为进行描述,这就是UICollectionViewLayout
。objective-c
而咱们主要讲UICollectionViewLayout,由于这不只是collectionView和tableView的最重要求的区别,也是整个UICollectionView的精髓所在。swift
@property (nonatomic) CGRect frame
@property (nonatomic) CGPoint center
@property (nonatomic) CGSize size
@property (nonatomic) CATransform3D transform3D
@property (nonatomic) CGFloat alpha
@property (nonatomic) NSInteger zIndex
@property (nonatomic, getter=isHidden) BOOL hidden
复制代码
能够看到,UICollectionViewLayoutAttributes的实例中包含了诸如边框,中心点,大小,形状,透明度,层次关系和是否隐藏等信息。数组
UICollectionViewLayout的功能为向UICollectionView提供布局信息,不只包括cell的布局信息,也包括追加视图和装饰视图的布局信息。实现一个自定义layout的常规作法是继承UICollectionViewLayout类,而后重载下列方法:oop
-(void)prepareLayout
prepare方法被自动调用,以保证layout实例的正确。
-(CGSize)collectionViewContentSize
返回collectionView的内容的尺寸
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
1. 返回rect中的全部的元素的布局属性
2. 返回的是包含UICollectionViewLayoutAttributes的NSArray
3. UICollectionViewLayoutAttributes能够是cell,追加视图或装饰视图的信息,经过不一样的UICollectionViewLayoutAttributes初始化方法能够获得不一样类型的UICollectionViewLayoutAttributes:
1)layoutAttributesForCellWithIndexPath:
2)layoutAttributesForSupplementaryViewOfKind:withIndexPath:
3)layoutAttributesForDecorationViewOfKind:withIndexPath:
-(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath
返回对应于indexPath的位置的cell的布局属性
-(UICollectionViewLayoutAttributes )layoutAttributesForSupplementaryViewOfKind:(NSString )kind atIndexPath:(NSIndexPath *)indexPath
返回对应于indexPath的位置的追加视图的布局属性,若是没有追加视图可不重载
-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString)decorationViewKind atIndexPath:(NSIndexPath )indexPath
返回对应于indexPath的位置的装饰视图的布局属性,若是没有装饰视图可不重载
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
当边界发生改变时,是否应该刷新布局。若是YES则在边界变化(通常是scroll到其余地方)时,将从新计算须要的布局信息。
调用顺序
复制代码
1)-(void)prepareLayout
a. collection view 只会在第一次layout的时候调用一次`-prepareLayout`,做为第一次通知layout实例对象的消息
b. collection view 会在 layout 对象 invalidated 以后而且requerying以前再次调用
c. 继承自UICollectionViewLayout都须要重写这个方法,通常都是在这个方法里面准备好`layoutAttributesForElements(in:)`这个方法要使用到的`UICollectionViewLayoutAttributes`数组。
2) -(CGSize) collectionViewContentSize
a. 肯定collectionView的全部内容的尺寸
b. 每一次移动collection view时都会调用这个方法,而且这个方法会被调用屡次
3)-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
初始的layout的外观将由该方法返回的UICollectionViewLayoutAttributes来决定。
4)在须要更新layout时,须要给当前layout发送
1)-invalidateLayout, 该消息会当即返回,而且预定在下一个loop的时候刷新当前layout
2)-prepareLayout,
3)依次再调用-collectionViewContentSize和-layoutAttributesForElementsInRect来生成更新后的布局。
复制代码