自定義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)。
以蘋果官方的lineLayout為例,這個是對UICollectionViewFlowLayout的擴充,
-(id)init
{
self = [super init];
if (self) {
self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(, 0.0, , 0.0);//上下邊距
self.minimumLineSpacing = 50.0;//行間距
}
return self;
}
這裡初始化一些資訊。
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
因為滑動放大,故這裡需要返回yes,實時重新整理layout。
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* array = [super layoutAttributesForElementsInRect:rect]; //可視rect
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size; //設定item的縮放
for (UICollectionViewLayoutAttributes* attributes in array) {
if (CGRectIntersectsRect(attributes.frame, rect)) {
CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;//item到中心點的距離
CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;//距離除以有效距離得到標準化距離
//距離小於有效距離才生效
NSLog(@"%f",distance);
if (ABS(distance) < ACTIVE_DISTANCE) {
CGFloat zoom = + ZOOM_FACTOR*( - ABS(normalizedDistance));//縮放率範圍1~1.3,與標準距離負相關
attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);//x,y軸方向變換
//attributes.zIndex = 0;
}
}
} return array;
}
這裡對item進行放大操作(因為該類是繼承自UICollectionViewFlowLayout,蘋果重寫了該方法,所以呼叫super可以拿到當前rect範圍內attributes,如果繼承自UICollectionViewLayout,呼叫super是什麼都拿不到的)。這裡的思想就是:距離螢幕中心超過一定距離(假設200,自己設定)開始放大,CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;item中心到螢幕中心點距離佔200的比例,
CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));
這樣便得到放大的倍數。
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
//proposedContentOffset是沒有對齊到網格時本來應該停下的位置 //計算出實際中心位置
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0); //取當前螢幕中的UICollectionViewLayoutAttributes
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
NSArray* array = [super layoutAttributesForElementsInRect:targetRect]; //對當前螢幕中的UICollectionViewLayoutAttributes逐個與螢幕中心進行比較,找出最接近中心的一個
for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenter - horizontalCenter;
}
} //返回調整好的point
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity 這個方法簡單理解可以當作是用來設定collectionView的偏移量的,計算當前螢幕哪個item中心點距離螢幕中心點近,就將該item拉到中心去。
個人理解,不對的地方還請見諒。