A WaterfallCollectionViewDemo 简便集成一个瀑布流图片效果的Demo 本来想做成一个控件,但看起来并没达成。 不过可以在50行左右代码集成出一个瀑布流效果图吧。 六部完成瀑布流。
import "FHYWaterView.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, EqualWidthLayOutDelegate>
self.waterView = [[FHYWaterView alloc] initWaterViewWithFrame:self.view.bounds];
EqualWidthLayOut *layOut = (EqualWidthLayOut *)self.waterView.collectionViewLayout;
[layOut setDelegate:self];
[self.waterView setDelegate:self];
[self.waterView setDataSource:self];
[self.view addSubview:_waterView];
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
FHYWaterViewModel *cellContent = self.contentArray[indexPath.row];
FHYWaterViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FHYWaterViewCell class]) forIndexPath:indexPath];
[cell setCellContent:cellContent];
return cell;
}
- (void)getDataFromNet{
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
[session GET:kTextUrlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *dataArray = (NSArray *)responseObject;
for (NSDictionary *dataDictionary in dataArray) {
//获取到数据后初始化模型并添加到数组中
NSString *imageUrl = [dataDictionary objectForKey:@"image_url"];
CGFloat imageWidth = [[dataDictionary objectForKey:@"image_width"] floatValue];
CGFloat imageHeight = [[dataDictionary objectForKey:@"image_height"] floatValue];
FHYWaterViewModel *cellContent = [[FHYWaterViewModel alloc] initWithUrlString:imageUrl imageWidth:imageWidth andImageHeight:imageHeight];
[self.contentArray addObject:cellContent];
}
[_waterView reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", kTextUrlString);
}];
}
- (CGFloat)collectionView:(UICollectionView *)view layout:(EqualWidthLayOut *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath{
FHYWaterViewModel *dataModel = [self.contentArray objectAtIndex:indexPath.row];
CGFloat kCellWidth = (kWidth - 10 * 3) / 2;
return kCellWidth * ( dataModel.height / dataModel.width );
}