自定義布局需要繼承類UICollectionViewLayout,然后重寫該類提供的一些方法:
1、- (void)prepareLayout; 準(zhǔn)備layout,可以初始化一些參數(shù),設(shè)置一些屬性。
2、- (CGSize)collectionViewContentSize;返回collectionView的contentSize。
3、- (NSArray? *)layoutAttributesForElementsInRect:(CGRect)rect; ? 返回rect(可視范圍)內(nèi)所有cell對(duì)應(yīng)的layoutAttribute。
4、 - (UICollectionViewLayoutAttributes)layoutAttributesForItemAtIndexPath:(NSIndexPath)indexPath;返回indexPath對(duì)應(yīng)item的layoutAtture對(duì)象。
5、- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;當(dāng)collectionView的大小發(fā)生改變時(shí),是否重新刷新layout:
6、 - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity;當(dāng)collectionView滾動(dòng)停止的時(shí)候停在什么位置。