關于UICollectionViewLayout的一些理解

- (void)prepareLayout; 

第一次加載layout、刷新layout、以及

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;

這個方法 return yes 時,會調用。

實現該方法后應該調用[super prepareLayout]保證初始化正確。用來準備一些布局所需要的信息。
該方法和init方法相似,但前者可能會被調用多次,所以一些不固定的計算(比如該計算和collectionView的尺寸相關),最好放在這里,以保證collectionView發生變化時,自定義CollectionView能做出正確的反應。

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; 

該方法用來返回rect范圍內的 cell supplementary 以及 decoration的布局屬性layoutAttributes(這里保存著她們的尺寸,位置,indexPath等等)

- (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(200, 0.0, 200, 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 = 1 + ZOOM_FACTOR*(1 - 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拉到中心去。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容