iOS 瀑布流封裝

代碼地址以下:<br>http://www.demodashi.com/demo/12284.htmlhtml

###1、效果預覽 瀑布流.gif佈局

功能描述:WSLWaterFlowLayout 是在繼承於UICollectionViewLayout的基礎上封裝的帶頭腳視圖的瀑布流控件。目前支持豎向瀑布流(item等寬不等高、支持頭腳視圖)、水平瀑布流(item等高不等寬 不支持頭腳視圖)、豎向瀑布流( item等高不等寬、支持頭腳視圖)三種樣式的瀑布流佈局。atom

  • 前言 :近幾個月一直在忙公司的ChinaDaily和國務院項目,沒有抽出時間來寫,如今終於算是告一段落了,抽出時間來更一篇

2、實現:主要是重寫父類的幾個涉及佈局屬性的方法,在對應的佈局屬性方法中根據需求自定義視圖佈局屬性信息。詳情看示例

/** 初始化 生成每一個視圖的佈局信息*/
-(void)prepareLayout;
/** 決定一段區域全部cell和頭尾視圖的佈局屬性*/
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect ;
/** 返回indexPath位置cell對應的佈局屬性*/
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
/** 返回indexPath位置頭和腳視圖對應的佈局屬性*/
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
//返回內容高度
-(CGSize)collectionViewContentSize;

###3、 用法:注意遵循WSLWaterFlowLayoutDelegate協議,代理方法和TableView、collectionView的代理方法用法類似。 下面是WSLWaterFlowLayout.h中的屬性方法和代理方法,含義註釋的還算清晰:spa

typedef enum {
    WSLVerticalWaterFlow = 0, /** 豎向瀑布流 item等寬不等高 */
    WSLHorizontalWaterFlow = 1, /** 水平瀑布流 item等高不等寬 不支持頭腳視圖*/
    WSLVHWaterFlow = 2,  /** 豎向瀑布流 item等高不等寬 */
    WSLLineWaterFlow = 3 /** 線性佈局 待完成,敬請期待 */
} WSLFlowLayoutStyle;//樣式

@class WSLWaterFlowLayout;

@protocol WSLWaterFlowLayoutDelegate <NSObject>

/** 豎向瀑布流 item等寬不等高 */
-(CGFloat)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth;

/** 水平瀑布流 item等高不等寬 */
-(CGFloat)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath itemHeight:(CGFloat)itemHeight;

/** 豎向瀑布流 item等高不等寬  列數、行數無用 */
- (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

/** 頭視圖Size */
-(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section;
/** 腳視圖Size */
-(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForFooterViewInSection:(NSInteger)section;

@optional  //如下都有默認值
/** 列數*/
-(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout;
/** 行數*/
-(CGFloat)rowCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout;

/** 列間距*/
-(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout;
/** 行間距*/
-(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout;
/** 邊緣之間的間距*/
-(UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout;

@end

@interface WSLWaterFlowLayout : UICollectionViewLayout

/** delegate*/
@property (nonatomic, weak) id<WSLWaterFlowLayoutDelegate> delegate;
/** 瀑布流樣式*/
@property (nonatomic, assign) WSLFlowLayoutStyle  flowLayoutStyle;

@end

初始化僅三行代碼,只需設置代理和樣式,item的大小、頭腳視圖的大小、行列數以及間距均可以在對應樣式的代理方法中自定義,而後設置爲UICollectionView的自動流水佈局樣式,並結合UICollectionView的用法使用,詳情看示例:代理

WSLWaterFlowLayout * _flow = [[WSLWaterFlowLayout alloc] init];
    _flow.delegate = self;
    _flow.flowLayoutStyle = WSLVerticalWaterFlow;

###4、項目結構: code

iOS 瀑布流封裝htm

代碼地址以下:<br>http://www.demodashi.com/demo/12284.htmlblog

注:本文著做權歸做者,由demo大師代發,拒絕轉載,轉載須要做者受權繼承

相關文章
相關標籤/搜索