實(shí)現(xiàn)一個(gè)UICollectionView些楣,和UITableView類(lèi)似叁鉴,不過(guò)初始化的時(shí)候要傳入一個(gè)UICollectionViewLayout绵咱。
蘋(píng)果給UIcollectionview中的所有視圖都來(lái)自一個(gè)可重用的基類(lèi)备蚓,就是UICollectionReusableView空幻。意思就是UIcollectionViewCell和自定義SupplementaryView和DecorationView都是繼承者這個(gè)UICollectionReusableView類(lèi)來(lái)構(gòu)建自己的視圖烁峭。
UICollectionViewLayout是UICollectionView最精髓的部分,主要用來(lái)為collection view的元素提供位置信息和狀態(tài)信息。UICollectionViewLayout并不負(fù)責(zé)創(chuàng)建視圖约郁,這些視圖是collection view的數(shù)據(jù)源(data source)創(chuàng)建的缩挑。布局對(duì)象只不過(guò)定義了這些視圖的位置和大小。有了它我們可以自定義Cell的布局鬓梅,蘋(píng)果為我們?cè)O(shè)計(jì)了一套非常靈活通用的Layout子類(lèi)UICollectionViewFlowLayout供置。許多大神在博客上已經(jīng)對(duì)其進(jìn)行了詳細(xì)講解,本篇主要是記錄我對(duì)UICollectionViewFlowLayout的理解绽快。
需要注意的是芥丧,collection view進(jìn)行的布局限制在屏幕可見(jiàn)的范圍之內(nèi)。(注:即坊罢,即便創(chuàng)建了所有元素的布局對(duì)象续担,但是真正的布局只是在可見(jiàn)的范圍內(nèi),超出屏幕的部分沒(méi)有布局
UICollectionViewLayoutAttributes 布局屬性
該屬性可以拿到每個(gè)Cell活孩、SupplementaryView物遇、DecorationView的布局屬性,反過(guò)來(lái)憾儒,collection view 使用該屬性將cell和SupplementaryView放在其邊界內(nèi)询兴。
CGRect frame 布局視圖的frame簡(jiǎn)單明了
CGPoint center 視圖中心點(diǎn)
CGSize size 視圖尺寸
CATransform3D transform3D 這個(gè)屬性可以用來(lái)做酷炫的3D動(dòng)畫(huà)
CGAffineTransform transform 轉(zhuǎn)場(chǎng)屬性
CGFloat alpha 透明度
NSInteger zIndex 層級(jí),數(shù)字越大航夺,層級(jí)越高(最上面)蕉朵。
NSIndexPath *indexPath 如果是cell有對(duì)應(yīng)的indexPath
UICollectionElementCategory representedElementCategory 視圖標(biāo)記,是cell還是supplementary View或者decoration View
registerClass:forDecorationViewOfKind: 注冊(cè)decoration View
registerNib:forDecorationViewOfKind:
+(instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath 這個(gè)類(lèi)方法是decoration View布局的來(lái)源
-(nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath 與上一個(gè)方法結(jié)合得到decoration View布局
UICollectionViewLayoutInvalidationContext
當(dāng)UICollectionView調(diào)用invalidateLayoutWithContext:方法重新布局的時(shí)候配置該參數(shù)阳掐,指定哪些cell需要重新布局
UICollectionViewLayout
該類(lèi)指定UICollectionView中每個(gè)元素的布局
UICollectionViewLayout有3個(gè)分類(lèi)始衅,它們的大概作用如下:
- @interface UICollectionViewLayout (UISubclassingHooks) 這個(gè)擴(kuò)展類(lèi)中,都是用來(lái)布局UIcollectionView子視圖的
- @interface UICollectionViewLayout (UIUpdateSupportHooks) 用來(lái)布局刪除插入動(dòng)作
- @interface UICollectionViewLayout (UIReorderingSupportHooks) 移動(dòng)動(dòng)作布局
1缭保、UICollectionViewLayout (UISubclassingHooks)這個(gè)分類(lèi)主要做布局汛闸,在這個(gè)分類(lèi)里需要重寫(xiě)的方法 :
// 1、準(zhǔn)備布局艺骂,布局初始化一般在這里進(jìn)行
- (void)prepareLayout;
// 每當(dāng)collectionView邊界改變時(shí)便調(diào)用這個(gè)方法詢問(wèn) 是否 重新初始化布局 是則調(diào)用prepareLayout準(zhǔn)備布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;
// 2诸老、初始化布局時(shí)調(diào)用摊唇,返回特定rect(有可能是也可能不是可見(jiàn)rect)碌廓,每個(gè)cell和Supplementary和Decoration的布局屬性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect: (CGRect)rect;
// 當(dāng)滾動(dòng)停止時(shí)她倘,會(huì)調(diào)用該方法確定collectionView滾動(dòng)到的位置
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity;
//在使用UICollectionViewFlowLayout布局的時(shí)候窖维,有時(shí)會(huì)有特別的需求盅称,比如:
//當(dāng)一個(gè)cell滑動(dòng)到屏幕中點(diǎn)的時(shí)候放大鸡号,并且當(dāng)我停止滑動(dòng)時(shí)拷橘,能夠?qū)㈦x屏幕最近的cell居中舞虱。
//這四個(gè)方法就能輕松的完成這樣的事睦番。
- (CGSize)collectionViewContentSize;
返回collectionView內(nèi)容區(qū)的寬度和高度类茂,子類(lèi)必須重載該方法耍属,
返回值代表了所有內(nèi)容的寬度和高度,而不僅僅是可見(jiàn)范圍的巩检,
collectionView通過(guò)該信息配置它的滾動(dòng)范圍厚骗,默認(rèn)返回 CGSizeZero。
- (NSArray<__kindofUICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
返回UICollectionViewLayoutAttributes 類(lèi)型的數(shù)組兢哭,
UICollectionViewLayoutAttributes 對(duì)象包含cell或view的布局信息领舰。
子類(lèi)必須重載該方法,并返回該區(qū)域內(nèi)所有元素的布局信息厦瓢,包括cell,追加視圖和裝飾視圖提揍。
在創(chuàng)建UICollectionViewLayoutAttributes的時(shí)候,創(chuàng)建的是相應(yīng)元素類(lèi)型(cell, supplementary, decoration)的 attributes對(duì)象,比如:
+ (instancetype)layoutAttributesForCellWithIndexPath:(NSIndexPath *)indexPath;
+ (instancetype)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind withIndexPath:(NSIndexPath *)indexPath;
+ (instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath;
UICollectionView 根據(jù)不同的類(lèi)型區(qū)分屬性煮仇,并根據(jù)這些信息決定創(chuàng)建怎樣的視圖及如何進(jìn)行管理劳跃。
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
返回指定indexPath的item的布局信息。子類(lèi)必須重載該方法,該方法只能為cell提供布局信息浙垫,不能為補(bǔ)充視圖和裝飾視圖提供刨仑。
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
如果你的布局支持追加視圖的話,必須重載該方法夹姥,該方法返回的是追加視圖的布局信息杉武,
kind這個(gè)參數(shù)區(qū)分段頭還是段尾的,在collectionview注冊(cè)的時(shí)候回用到該參數(shù)辙售。
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath;
如果你的布局支持裝飾視圖的話轻抱,必須重載該方法,該方法返回的是裝飾視圖的布局信息旦部,
ecorationViewKind這個(gè)參數(shù)在collectionview注冊(cè)的時(shí)候回用到
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
該方法用來(lái)決定是否需要更新布局祈搜。如果collection view需要重新布局返回YES,否則返回NO,默認(rèn)返回值為NO。
子類(lèi)重載該方法的時(shí)候士八,基于是否collectionview的bounds的改變會(huì)引發(fā)cell和view布局的改變(滾動(dòng)屏幕cell或者view會(huì)不會(huì)變)容燕,給出正確的返回值。
如果返回YES婚度,UICollectionView通過(guò)調(diào)用invalidateLayoutWithContext方法使原來(lái)的layout失效
這些方法為collection view 在屏幕上布局提供了最基礎(chǔ)的布局信息蘸秘,如果你不想為追加視圖和裝飾視圖布局,可以不去重載相應(yīng)的方法蝗茁。
2醋虏、UICollectionViewLayout (UIUpdateSupportHooks)這個(gè)分類(lèi)主要做刪除插入等動(dòng)作,
- 當(dāng)collection view的數(shù)據(jù)發(fā)生改變的時(shí)候哮翘,比如插入或者刪除 item的時(shí)候灰粮,collection view將會(huì)要求布局對(duì)象更新相應(yīng)的布局信息。添加忍坷、刪除 items時(shí)都必須更新相應(yīng)的布局信息以便反映元素最新的位置。
- 而collection view刪除或者添加元素的時(shí)候,將會(huì)調(diào)用一些不同的方法佩研,你應(yīng)該重載以便提供正確的布局信息.
在這個(gè)分類(lèi)里需要重寫(xiě)的方法 :
- (void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems;
做一些和布局相關(guān)的準(zhǔn)備工作柑肴。該方法在插入刪除動(dòng)作之前被調(diào)用
/** 一般來(lái)說(shuō),我們對(duì)布局屬性從初始狀態(tài)到結(jié)束狀態(tài)進(jìn)行線性插值來(lái)計(jì)算 collection view 的動(dòng)畫(huà)參數(shù)旬薯。
然而晰骑,新插入或者刪除的元素并沒(méi)有最初或最終狀態(tài)來(lái)進(jìn)行插值。
要計(jì)算這樣的 cells 的動(dòng)畫(huà)绊序,collection view 將通過(guò) initialLayoutAttributesForAppearingItemAtIndexPath:
以及 finalLayoutAttributesForAppearingItemAtIndexPath: 方法來(lái)詢問(wèn)其布局對(duì)象硕舆,
以獲取最初的和最后的屬性。
下面這兩個(gè)方法是成對(duì)出現(xiàn)的骤公,一個(gè)是在在屏幕上出現(xiàn)之前調(diào)用抚官,一個(gè)是在屏幕上出現(xiàn)之后調(diào)用
在[collecView reloadData]或者performBatchUpdates:completion:調(diào)用的時(shí)候
只要是有刷新的效果,他們就會(huì)被調(diào)用多次阶捆,視圖不斷的消失(失效凌节,被摧毀)出現(xiàn)(重組 被創(chuàng)建或者回收利用)*/
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//在一個(gè) item被插入到collection view 的時(shí)候,返回開(kāi)始的布局信息洒试。
//這個(gè)方法在 prepareForCollectionViewUpdates:之后和finalizeCollectionViewUpdates 之前調(diào)用倍奢。
//UICollectionView將會(huì)使用該布局信息作為動(dòng)畫(huà)的起點(diǎn)(結(jié)束點(diǎn)是該item在UICollectionView的最新的位置)。
//如果返回為nil垒棋,布局對(duì)象將用item的最終的attributes 作為動(dòng)畫(huà)的起點(diǎn)和終點(diǎn)卒煞。
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//返回值是item即將從collection view移除時(shí)候的布局信息,對(duì)即將刪除的item來(lái)講叼架,
//該方法在 prepareForCollectionViewUpdates: 之后和finalizeCollectionViewUpdates 之前調(diào)用畔裕。
//在該方法中返回的布局信息描包含 item的狀態(tài)信息和位置信息。
//UICollectionView將會(huì)把該信息作為動(dòng)畫(huà)的終點(diǎn)(起點(diǎn)是item當(dāng)前的位置)碉碉。
//如果返回為nil的話柴钻,布局對(duì)象將會(huì)把當(dāng)前的attribute,作為動(dòng)畫(huà)的起點(diǎn)和終點(diǎn)垢粮。
也可以重載
- (void)finalizeCollectionViewUpdates;
通過(guò)該方法添加一些動(dòng)畫(huà)到block贴届,或者做一些和最終布局相關(guān)的工作。更新結(jié)束后調(diào)用
//返回值是追加視圖插入U(xiǎn)ICollectionView時(shí)的布局信息蜡吧。以下方法使用同initialLayoutAttributesForAppearingItemAtIndexPath:
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
//返回值是裝飾視圖插入U(xiǎn)ICollectionView時(shí)的布局信息毫蚓。
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
//返回值是追加視圖刪除UICollectionView時(shí)的布局信息。
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
//返回值是裝飾視圖刪除UICollectionView時(shí)的布局信息昔善。
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
舉例:demo下載(https://github.com/mpospese/CircleLayout)
demo中當(dāng)調(diào)用
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:tappedCellPath]];
} completion:nil];
方法刪除cell的時(shí)候元潘,在UICollectionViewLayout中,以下方法會(huì)被先后調(diào)用:
1君仆、- (void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems;
2翩概、- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
3牲距、- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
4、- (void)finalizeCollectionViewUpdates
其中2钥庇、3方法會(huì)反復(fù)調(diào)用多次牍鞠。視圖不斷的消失(失效,被摧毀)出現(xiàn)(重組 被創(chuàng)建或者回收利用)
** 3评姨、UICollectionViewLayout (UIReorderingSupportHooks) 移動(dòng)動(dòng)作布局**
-
對(duì)于移動(dòng)的元素,UICollectionView提供了標(biāo)準(zhǔn)的方法獲取更新后的布局信息难述。
- (NSIndexPath *)targetIndexPathForInteractivelyMovingItem:(NSIndexPath *)previousIndexPath withPosition:(CGPoint)position NS_AVAILABLE_IOS(9_0);
根據(jù)item在UICollectionView中的位置獲取該item的NSIndexPath。
第一個(gè)參數(shù)該item原來(lái)的index path吐句,第二個(gè)參數(shù)是item在collection view中的位置胁后。
在item移動(dòng)的過(guò)程中,該方法將UICollectionView中的location映射成相應(yīng) NSIndexPaths嗦枢。該方法的默認(rèn)是顯示攀芯,查找指定位置的已經(jīng)存在的cell,返回該cell的NSIndexPaths 净宵。如果在相同的位置有多個(gè)cell敲才,該方法默認(rèn)返回最上層的cell。
- (UICollectionViewLayoutAttributes *)layoutAttributesForInteractivelyMovingItemAtIndexPath:(NSIndexPath *)indexPath withTargetPosition:(CGPoint)position NS_AVAILABLE_IOS(9_0);
當(dāng)item在手勢(shì)交互下移動(dòng)時(shí)择葡,通過(guò)該方法返回這個(gè)item布局的attributes 紧武。
默認(rèn)實(shí)現(xiàn)是,復(fù)制已存在的attributes敏储,改變attributes兩個(gè)值阻星,一個(gè)是中心點(diǎn)center;另一個(gè)是z軸的坐標(biāo)值已添,設(shè)置成最大值妥箕。
所以該item在collection view的最上層。子類(lèi)重載該方法更舞,可以按照自己的需求更改attributes畦幢,
首先需要調(diào)用super類(lèi)獲取attributes,然后自定義返回的數(shù)據(jù)結(jié)構(gòu)。
- (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缆蝉。
基本都是一看就知道它是干嘛的宇葱。
但是如果每個(gè)cell布局樣式都不一樣的話,就要在代理方法里面設(shè)置這些屬性
也是顧名思義的刊头。在子類(lèi)化UICollectionViewLayout之前黍瞧,應(yīng)該先了解UICollectionViewFlowLayout,看看它是否已經(jīng)能夠滿足你的布局需求原杂。
參考:http://blog.csdn.net/qq_30513483/article/details/51611653