iOS瀑布流
时间:2022-06-09
本文章向大家介绍iOS瀑布流,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
瀑布流Demo
瀑布流截图.gif
使用UICollectionView实现瀑布流
自定义UICollectionViewLayout中的主要代码:
YJWaterFlowLayout.h中代码:
#import <UIKit/UIKit.h>
@class YJWaterFlowLayout;
@protocol YJWaterFlowLayoutDelegate <NSObject>
@required
-(CGFloat)waterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout heightForItemAtIndexPath:(NSUInteger)index itemWidth:(CGFloat)itemWidth;
@optional
/** 列数*/
-(CGFloat)columnCountInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
/** 列间距*/
-(CGFloat)columnMarginInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
/** 行间距*/
-(CGFloat)rowMarginInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
/** 边缘之间的间距*/
-(UIEdgeInsets)edgeInsetInWaterFlowLayout:(YJWaterFlowLayout *)waterFlowLayout;
@end
@interface YJWaterFlowLayout : UICollectionViewLayout
/** delegate*/
@property (nonatomic, weak) id<YJWaterFlowLayoutDelegate> delegate;
@end
YJWaterFlowLayout.m文件代码:
#import "YJWaterFlowLayout.h"
/** 默认的列数*/
static const NSInteger YJDefaultColumeCount = 3;
/** 每一列之间的间距*/
static const NSInteger YJDefaultColumeMargin = 10;
/** 每一行之间的间距*/
static const CGFloat YJDefaultRowMargin = 10;
/** 边缘之间的间距*/
static const UIEdgeInsets YJDefaultEdgeInset = {10, 10, 10, 10};
@interface YJWaterFlowLayout ()
/** 存放所有cell的布局属性*/
@property (strong, nonatomic) NSMutableArray *attrsArray;
/** 存放每一列的最大y值*/
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 每一行之间的间距*/
-(CGFloat)rowMargin;
/** 每一列之间的间距*/
-(CGFloat)columnMargin;
/** 列数*/
-(NSInteger)columnCount;
/** 边缘之间的间距*/
-(UIEdgeInsets)edgeInsets;
/** 内容的高度*/
@property (nonatomic, assign) CGFloat maxColumnHeight;
@end
@implementation YJWaterFlowLayout
#pragma mark 代理方法处理
-(CGFloat)rowMargin {
if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFlowLayout:)]) {
return [self.delegate rowMarginInWaterFlowLayout:self];
} else {
return YJDefaultRowMargin;
}
}
-(CGFloat)columnMargin {
if ([self.delegate respondsToSelector:@selector(columnMarginInWaterFlowLayout:)]) {
return [self.delegate columnMarginInWaterFlowLayout:self];
} else {
return YJDefaultColumeMargin;
}
}
-(NSInteger)columnCount {
if ([self.delegate respondsToSelector:@selector(columnCountInWaterFlowLayout:)]) {
return [self.delegate columnCountInWaterFlowLayout:self];
} else {
return YJDefaultColumeCount;
}
}
-(UIEdgeInsets)edgeInsets {
if ([self.delegate respondsToSelector:@selector(edgeInsetInWaterFlowLayout:)]) {
return [self.delegate edgeInsetInWaterFlowLayout:self];
} else {
return YJDefaultEdgeInset;
}
}
#pragma mark - 懒加载
- (NSMutableArray *)columnHeights {
if (!_columnHeights) {
_columnHeights = [NSMutableArray array];
}
return _columnHeights;
}
-(NSMutableArray *)attrsArray {
if (_attrsArray == nil) {
_attrsArray = [NSMutableArray array];
}
return _attrsArray;
}
/** 初始化*/
-(void)prepareLayout {
[super prepareLayout];
//清除以前计算的所有高度
self.maxColumnHeight = 0;
[self.columnHeights removeAllObjects];
for (NSInteger i = 0; i < self.columnCount; i++) {
[self.columnHeights addObject:@(self.edgeInsets.top)];
}
//清除之前数组
[self.attrsArray removeAllObjects];
//开始创建每一个cell
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (NSInteger i = 0; i < count; i++) {
//创建位置
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
//获取indexPath位置cell对应的成员属性
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.attrsArray addObject:attrs];
}
}
/** 决定cell的排布*/
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
return self.attrsArray;
}
/** 返回indexPath位置cell对应的布局属性*/
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
//设置布局属性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
//collectionView的宽度
CGFloat collectionW = self.collectionView.frame.size.width;
//设置布局属性的frame
CGFloat w = (collectionW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
CGFloat h = [self.delegate waterFlowLayout:self heightForItemAtIndexPath:indexPath.item itemWidth:w];
//找出高度最短的那一列
NSInteger destColumn = 0;
CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 1; i < self.columnCount; i++) {
//取出第i列
CGFloat columnHeight = [self.columnHeights[i] doubleValue];
if (minColumnHeight > columnHeight) {
minColumnHeight = columnHeight;
destColumn = i;
}
}
CGFloat x = self.edgeInsets.left + destColumn * (w + self.rowMargin);
CGFloat y = minColumnHeight;
if (y != self.edgeInsets.top) {
if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFlowLayout:)]) {
y += [self.delegate rowMarginInWaterFlowLayout:self];
} else {
y += self.rowMargin;
}
}
attrs.frame = CGRectMake(x, y, w, h);
//更新最短那列的高度
self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
//记录内容的高度
CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
if (self.maxColumnHeight < columnHeight) {
self.maxColumnHeight = columnHeight;
}
return attrs;
}
-(CGSize)collectionViewContentSize {
return CGSizeMake(0, self.maxColumnHeight + self.edgeInsets.bottom);
}
@end
瀑布流Demo地址
- Linux自动每日备份
- Java进阶10 内存管理与垃圾回收
- Md5加密秘钥加密哈希加密
- JavaSE(十二)之IO流的字节流(一)
- 基于ASP.MVC票据FormsAuthenticationTicket身份认证
- AI需要大数据,而大数据也需要AI
- Java进阶09 事件响应
- JavaSE(十一)之异常处理详解
- 微信开发扫一扫功能并且屏蔽分享菜单
- HTML语义化:HTML5新标签——template
- 纸上谈兵: 左倾堆 (leftist heap)
- 统计Go, Go, Go
- 前端构建:Source Maps详解
- 解决YUM下Loaded plugins: fastestmirror Determining fastest mirrors 的问题
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- pipeAsyncFunctions
- 【python-leetcode287-循环排序】寻找重复的数
- 聊聊claudb的list command
- 【python-leetcode448-循环排序】找到所有数组中消失的数字
- input/change/composition/keydown事件详解
- isSameDate
- 【python-leetcode57-区间合并】插入区间
- 【python-leetcode56-区间合并】合并区间
- 【python-leetcode876-快慢指针】链表的中间节点
- vuejs之springboot+vue+element-ui之分页显示相关信息
- vuejs之vue和springboot后端进行通信
- vuejs之springboot+vue+element-ui之添加信息
- vuejs之使用axios发送http请求
- vuejs小例子之记事本
- c语言之在main中获取命令行的参数