iOS 瀑布流布局

更具體的代碼見文末demo地址


效果圖

效果圖.gif

使用方式

可以通過pod導(dǎo)入,也可以下載demo后把LYWaterFlowLayout文件夾導(dǎo)入。

pod 'LYWaterFlowLayout', '~> 0.0.2'
如果pod repo update后還pod search不到,可以運(yùn)行如下命令清下緩存后應(yīng)該就可以了。
rm ~/Library/Caches/CocoaPods/search_index.json
在使用的控制器中,
    
遵守代理<UICollectionViewDelegate, UICollectionViewDataSource,LYWaterFlowLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *datas;

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self configData];
    [self creatUI];
    
    
}

-(void)configData{
    
    // 瀑布流假數(shù)據(jù)
    self.datas = [NSMutableArray array];
    for (int i = 0; i < 25; i++) {
        LYItemModel *itemModel = [[LYItemModel alloc] init];
        itemModel.title = [NSString stringWithFormat:@"測試標(biāo)題%d", i];
        itemModel.itemSizeScale = i%3 + 0.5; // item比例
        [self.datas addObject:itemModel];
    }

}

-(void)creatUI{
    
    LYWaterFlowLayout *layout = [[LYWaterFlowLayout alloc] init];
    layout.columnMargin = 10;
    layout.rowMargin = 10;
    layout.columnCount = 3;
    layout.endgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.degelate = self;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[LYCollectionViewCell class] forCellWithReuseIdentifier:cellIndentifier];
    [self.view addSubview:self.collectionView];


}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datas.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
    LYItemModel *itemModel = self.datas[indexPath.row];
    cell.backgroundColor = [UIColor yellowColor];
    cell.titleLable.text = itemModel.title;
    return cell;
}
-(CGFloat)Flow:(LYWaterFlowLayout *)Flow heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath{
    // width是根據(jù)列數(shù)和列間距計(jì)算好的item寬度,可以再根據(jù)item的比例來計(jì)算高度
    LYItemModel *itemModel = self.datas[indexPath.row];
    return width*itemModel.itemSizeScale;
}


demo下載https://github.com/zhuzi55/LYWaterFlowLayout.git

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

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

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