iOS实现水平方向瀑布流
2019/7/9 23:15:35
本文主要是介绍iOS实现水平方向瀑布流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
效果
源码:https://github.com/YouXianMing/Animations
// // GridFlowLayoutViewController.m // Animations // // Created by YouXianMing on 16/5/5. // Copyright © 2016年 YouXianMing. All rights reserved. // #import "GridFlowLayoutViewController.h" #import "UIView+SetRect.h" #import "GridLayout.h" #import "FlowStyleCell.h" #import "FileManager.h" #import "NSString+MD5.h" #import "NSData+JSONData.h" #import "ResponseData.h" #import "Math.h" #import "GCD.h" static NSString *picturesSource = @"http://www.duitang.com/album/1733789/masn/p/0/50/"; @interface GridFlowLayoutViewController () <UICollectionViewDataSource, UICollectionViewDelegate, GridLayoutDelegate> @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic) CGFloat rowHeight; @property (nonatomic, strong) NSMutableArray *datas; @property (nonatomic, strong) ResponseData *picturesData; @property (nonatomic, strong) NSMutableArray <WaterfallPictureModel *> *dataSource; @end @implementation GridFlowLayoutViewController - (void)setup { [super setup]; _dataSource = [NSMutableArray new]; // 初始化布局文件 CGFloat gap = 1; NSInteger rowCount = arc4random() % 3 + 2; _rowHeight = (self.contentView.height - (rowCount + 1) * gap) / (CGFloat)rowCount; GridLayout *layout = [GridLayout new]; layout.manager.edgeInsets = UIEdgeInsetsMake(gap, gap, gap, gap); layout.manager.gap = gap; layout.delegate = self; NSMutableArray *rowHeights = [NSMutableArray array]; for (int i = 0; i < rowCount; i++) { [rowHeights addObject:@(_rowHeight)]; } layout.manager.rowHeights = rowHeights; self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.backgroundColor = [UIColor clearColor]; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.alpha = 0; [self.collectionView registerClass:[FlowStyleCell class] forCellWithReuseIdentifier:@"FlowStyleCell"]; [self.contentView addSubview:self.collectionView]; // 获取数据 [GCDQueue executeInGlobalQueue:^{ NSString *string = [picturesSource lowerMD532BitString]; NSString *realFilePath = [FileManager theRealFilePath:[NSString stringWithFormat:@"~/Documents/%@", string]]; NSData *data = nil; if ([FileManager fileExistWithRealFilePath:realFilePath] == NO) { data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:picturesSource]]; [data writeToFile:realFilePath atomically:YES]; } else { data = [NSData dataWithContentsOfFile:realFilePath]; } NSDictionary *dataDic = [data toListProperty]; [GCDQueue executeInMainQueue:^{ self.picturesData = [[ResponseData alloc] initWithDictionary:dataDic]; if (self.picturesData.success.integerValue == 1) { for (int i = 0; i < self.picturesData.data.blogs.count; i++) { [_dataSource addObject:self.picturesData.data.blogs[i]]; } [_collectionView reloadData]; [UIView animateWithDuration:0.5f animations:^{ _collectionView.alpha = 1.f; }]; } }]; }]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { WaterfallPictureModel *pictureModel = _dataSource[indexPath.row]; FlowStyleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FlowStyleCell" forIndexPath:indexPath]; cell.indexPath = indexPath; cell.data = pictureModel; cell.rowHeight = _rowHeight; [cell loadContent]; return cell; } - (CGFloat)itemWidthWithIndexPath:(NSIndexPath *)indexPath { WaterfallPictureModel *pictureModel = _dataSource[indexPath.row]; return [Math resetFromSize:CGSizeMake(pictureModel.iwd.floatValue, pictureModel.iht.floatValue) withFixedHeight:_rowHeight].width; } @end
细节
继承UICollectionViewLayout
重载UICollectionViewLayout的四个方法
部分实现细节
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于iOS实现水平方向瀑布流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-12Axios库资料:新手入门必读教程
- 2024-11-11Axios库项目实战:新手入门教程
- 2024-09-29Axios库教程:初学者必备指南
- 2024-08-29Axios库资料:新手入门指南与基本使用教程
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创