流式布局過去也玩很多種,今天來總結(jié)一下瀑布流,市面上應(yīng)該是不那么流行了,不過無奈產(chǎn)品經(jīng)理喜歡呀~
自定義布局
其實(shí)需要使用到
UICollectionViewLayout
a、其決定UICollectionView如何將單元格顯示實(shí)現(xiàn)流程
1、- (void)prepareLayout;運(yùn)算所有單元格位置
2、- (CGSize)collectionViewContentSize;返回運(yùn)算后視圖的總大小
3、- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;返回運(yùn)算后的單元格布局屬性(包含懸停的實(shí)現(xiàn)部分)
4、- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;單元格進(jìn)行布局時(shí),返回的是要獲取的布局屬性
5、(nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;同上,不同在于,非單元格、是頭尾視圖
6、- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath;同上,是裝飾視圖,若沒有不需要重載
7、- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;當(dāng)邊界發(fā)生變化時(shí),決定是否刷新ok,流程介紹完畢,可以開始有作為了??~
懸停關(guān)鍵點(diǎn)
- 在于流程3中,保持y坐標(biāo)對(duì)齊頂部
CGFloat headerHeight = CGRectGetHeight(attriture.frame);
CGRect frame = attriture.frame;
frame.origin.y = MIN(MAX(self.collectionView.contentOffset.y, CGRectGetMinY(itemAttribute.frame)-headerHeight-contentInsetOfSection.top),
CGRectGetMinY(itemAttribute.frame)+[_heightOfSections[section] floatValue]);
attriture.frame = frame;
attriture.zIndex = (NSIntegerMax/2)+section;
- 在于流程7中,需要刷新視圖,保證對(duì)齊
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
if (_sectionHeadersPinToVisibleBounds) {
return YES;
}
return [super shouldInvalidateLayoutForBoundsChange:newBounds];
}
預(yù)覽圖

也可以下載項(xiàng)目查看~
# [WaterHoverLayout]