Core Image Programming Guide--图像编程指南
javascript
-(CGSize)collectionViewContentSizehtml
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBoundsjava
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path{}//返回每一个cell的布局属性ios
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect //返回全部cell的布局属性objective-c
关于自定义UICollectionViewLayout的一点我的理解编程
自定义UICollectionView,主要会用到如下几个方法:
- (void)prepareLayout; 第一次加载layout、刷新layout、以及- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;这个方法返回yes时,会调用。这是苹果官方的说明The collection view calls -prepareLayout once at its first layout as the first message to the layout instance. The collection view calls -prepareLayout again after layout is invalidated and before requerying the layout information. Subclasses should always call super if they override。实现该方法后应该调用[super prepareLayout]保证初始化正确。该方法用来准备一些布局所须要的信息。该方法和init方法类似,但该方法可能会被调用屡次,因此一些不固定的计算(好比该计算和collectionView的尺寸相关),最好放在这里,以保证collectionView发生变化时,自定义CollectionView能作出正确的反应。
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; 该方法用来返回rect范围内的 cell supplementary 以及 decoration的布局属性layoutAttributes(这里保存着她们的尺寸,位置,indexPath等等),若是你的布局都在一个屏幕内 活着 没有复杂的计算,我以为这里能够返回所有的属性数组,若是涉及到复杂计算,应该进行判断,返回区域内的属性数组,有时候为了方便直接返回了所有的属性数组,不影响布局但可能会影响性能(若是你的item一屏幕显示不完,那么这个方法会调用屡次,当全部的item都加载完毕后,在滑动collectionView时不会调用该方法的)。
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath; 该方法不是必须实现的,即使你实现了,咱们对collectionView的任何操做,也不会致使系统主动调用该方法。该方法一般用来定制某个IndexPath的item的属性。固然咱们也能够重写这个方法,将布局时相关的属性设置放在这里,在- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect 或者 - (void)prepareLayout 中 须要建立用来返回给系统的属性数组 主动调用这个方法,并添加带可变数组中去返回给系统。固然咱们也能够在 - (void)prepareLayout 中 经过[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] 获取 每一个indexPath的attributes,在- (void)prepareLayout中设置全部item的属性。看需求以及我的喜欢。
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds; 用来刷新layout的,当咱们返回yes的时候。若是咱们的需求不须要实时的刷新layout,那么最好判断newBounds 和 咱们的collectionView的bounds是否相同,不一样时返回yes;(例如苹果官方的lineLayout,由于每次滑动都要放大item,因此这了就直接返回yes)。数组
调试:案例学习app