iOS | 關(guān)于瀑布流附帶懸停的簡易實(shí)現(xiàn)

流式布局過去也玩很多種,今天來總結(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]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容