博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
水平方向瀑布流
阅读量:5223 次
发布时间:2019-06-14

本文共 4698 字,大约阅读时间需要 15 分钟。

水平方向瀑布流

 

效果

 

源码

////  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 () 
@property (nonatomic, strong) UICollectionView *collectionView;@property (nonatomic) CGFloat rowHeight;@property (nonatomic, strong) NSMutableArray *datas;@property (nonatomic, strong) ResponseData *picturesData;@property (nonatomic, strong) NSMutableArray
*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的四个方法

部分实现细节

 

转载于:https://www.cnblogs.com/YouXianMing/p/5460903.html

你可能感兴趣的文章
Foxmail:导入联系人
查看>>
vue:axios二次封装,接口统一存放
查看>>
vue中router与route的区别
查看>>
js 时间对象方法
查看>>
网络请求返回HTTP状态码(404,400,500)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
图片加载失败显示默认图片占位符
查看>>
【★】浅谈计算机与随机数
查看>>
《代码阅读方法与实现》阅读笔记一
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
Java中正则表达式的使用
查看>>
算法之搜索篇
查看>>
新的开始
查看>>
java Facade模式
查看>>
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>
SpringAop与AspectJ
查看>>