YBShopLayout.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. //
  2. // YBShopLayout.m
  3. // YBLive
  4. //
  5. // Created by ybRRR on 2022/4/19.
  6. // Copyright © 2022 cat. All rights reserved.
  7. //
  8. #import "YBShopLayout.h"
  9. /** 默认的列数*/
  10. static const NSInteger YBDefaultColumeCount = 2;
  11. /** 默认的行数*/
  12. static const NSInteger YBDefaultRowCount = 5;
  13. /** 每一列之间的间距*/
  14. static const NSInteger YBDefaultColumeMargin = 10;
  15. /** 每一行之间的间距*/
  16. static const CGFloat YBDefaultRowMargin = 10;
  17. /** 边缘之间的间距*/
  18. static const UIEdgeInsets YBDefaultEdgeInset = {10, 10, 10, 10};
  19. ///** 每一行之间的间距*/
  20. //static const CGSize YBDefaultHeaderSize = CGSizeMake(0, 66);
  21. ///** 每一行之间的间距*/
  22. //static const CGSize YBDefaultFooterSize = CGSizeMake(0, 66);
  23. @interface YBShopLayout ()
  24. /** 存放所有cell的布局属性*/
  25. @property (strong, nonatomic) NSMutableArray *attrsArray;
  26. /** 存放每一列的最大y值*/
  27. @property (nonatomic, strong) NSMutableArray *columnHeights;
  28. /** 存放每一行的最大x值*/
  29. @property (nonatomic, strong) NSMutableArray *rowWidths;
  30. /** 内容的高度*/
  31. @property (nonatomic, assign) CGFloat maxColumnHeight;
  32. /** 内容的宽度*/
  33. @property (nonatomic, assign) CGFloat maxRowWidth;
  34. /** 列数*/
  35. -(NSInteger)columnCount;
  36. /** 行数*/
  37. -(NSInteger)rowCount;
  38. /** 每一行之间的间距*/
  39. -(CGFloat)rowMargin;
  40. /** 每一列之间的间距*/
  41. -(CGFloat)columnMargin;
  42. /** 边缘之间的间距*/
  43. -(UIEdgeInsets)edgeInsets;
  44. @end
  45. @implementation YBShopLayout
  46. #pragma mark item属性配置
  47. -(CGFloat)columnMargin {
  48. if ([self.delegate respondsToSelector:@selector(columnMarginInWaterFlowLayout:)]) {
  49. return [self.delegate columnMarginInWaterFlowLayout:self];
  50. } else {
  51. return YBDefaultColumeMargin;
  52. }
  53. }
  54. -(CGFloat)rowMargin {
  55. if ([self.delegate respondsToSelector:@selector(rowMarginInWaterFlowLayout:)]) {
  56. return [self.delegate rowMarginInWaterFlowLayout:self];
  57. } else {
  58. return YBDefaultRowMargin;
  59. }
  60. }
  61. -(NSInteger)columnCount {
  62. if ([self.delegate respondsToSelector:@selector(columnCountInWaterFlowLayout:)]) {
  63. return [self.delegate columnCountInWaterFlowLayout:self];
  64. } else {
  65. return YBDefaultColumeCount;
  66. }
  67. }
  68. -(NSInteger)rowCount{
  69. if ([self.delegate respondsToSelector:@selector(rowCountInWaterFlowLayout:)]) {
  70. return [self.delegate rowCountInWaterFlowLayout:self];
  71. } else {
  72. return YBDefaultRowCount;
  73. }
  74. }
  75. -(UIEdgeInsets)edgeInsets {
  76. if ([self.delegate respondsToSelector:@selector(edgeInsetInWaterFlowLayout:)]) {
  77. return [self.delegate edgeInsetInWaterFlowLayout:self];
  78. } else {
  79. return YBDefaultEdgeInset;
  80. }
  81. }
  82. #pragma mark - 懒加载
  83. - (NSMutableArray *)columnHeights {
  84. if (!_columnHeights) {
  85. _columnHeights = [NSMutableArray array];
  86. }
  87. return _columnHeights;
  88. }
  89. - (NSMutableArray *)rowWidths {
  90. if (!_rowWidths) {
  91. _rowWidths = [NSMutableArray array];
  92. }
  93. return _rowWidths;
  94. }
  95. -(NSMutableArray *)attrsArray {
  96. if (_attrsArray == nil) {
  97. _attrsArray = [NSMutableArray array];
  98. }
  99. return _attrsArray;
  100. }
  101. #pragma mark - 重写系统方法
  102. /** 初始化 生成每个视图的布局信息*/
  103. -(void)prepareLayout {
  104. [super prepareLayout];
  105. if (self.flowLayoutStyle == YBWaterFlowVerticalEqualWidth) {
  106. //清除以前计算的所有高度
  107. self.maxColumnHeight = 0;
  108. [self.columnHeights removeAllObjects];
  109. for (NSInteger i = 0; i < self.columnCount; i++) {
  110. [self.columnHeights addObject:@(self.edgeInsets.top)];
  111. }
  112. }else if (self.flowLayoutStyle == YBWaterFlowHorizontalEqualHeight){
  113. //清除以前计算的所有宽度
  114. self.maxRowWidth = 0;
  115. [self.rowWidths removeAllObjects];
  116. for (NSInteger i = 0; i < self.rowCount; i++) {
  117. [self.rowWidths addObject:@(self.edgeInsets.left)];
  118. }
  119. }else if (self.flowLayoutStyle == YBWaterFlowVerticalEqualHeight || self.flowLayoutStyle == YBLineWaterFlow){
  120. //记录最后一个的内容的横坐标和纵坐标
  121. self.maxColumnHeight = 0;
  122. [self.columnHeights removeAllObjects];
  123. [self.columnHeights addObject:@(self.edgeInsets.top)];
  124. self.maxRowWidth = 0;
  125. [self.rowWidths removeAllObjects];
  126. [self.rowWidths addObject:@(self.edgeInsets.left)];
  127. }else if(self.flowLayoutStyle == YBWaterFlowHorizontalGrid){
  128. //记录最后一个的内容的横坐标和纵坐标
  129. self.maxColumnHeight = 0;
  130. self.maxRowWidth = 0;
  131. [self.rowWidths removeAllObjects];
  132. for (NSInteger i = 0; i < 2; i++) {
  133. [self.rowWidths addObject:@(self.edgeInsets.left)];
  134. }
  135. }
  136. //清除之前数组
  137. [self.attrsArray removeAllObjects];
  138. //开始创建每一组cell的布局属性
  139. NSInteger sectionCount = [self.collectionView numberOfSections];
  140. for(NSInteger section = 0; section < sectionCount; section++){
  141. //获取每一组头视图header的UICollectionViewLayoutAttributes
  142. if([self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForHeaderViewInSection:)]){
  143. UICollectionViewLayoutAttributes *headerAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
  144. [self.attrsArray addObject:headerAttrs];
  145. }
  146. //开始创建组内的每一个cell的布局属性
  147. NSInteger rowCount = [self.collectionView numberOfItemsInSection:section];
  148. for (NSInteger row = 0; row < rowCount; row++) {
  149. //创建位置
  150. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:section];
  151. //获取indexPath位置cell对应的布局属性
  152. UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
  153. [self.attrsArray addObject:attrs];
  154. }
  155. //获取每一组脚视图footer的UICollectionViewLayoutAttributes
  156. if([self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForFooterViewInSection:)]){
  157. UICollectionViewLayoutAttributes *footerAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
  158. [self.attrsArray addObject:footerAttrs];
  159. }
  160. }
  161. }
  162. /** 决定一段区域所有cell和头尾视图的布局属性*/
  163. -(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  164. return self.attrsArray;
  165. }
  166. /** 返回indexPath位置cell对应的布局属性*/
  167. -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  168. //设置布局属性
  169. UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  170. if (self.flowLayoutStyle == YBWaterFlowVerticalEqualWidth) {
  171. attrs.frame = [self itemFrameOfVerticalWaterFlow:indexPath];
  172. }else if (self.flowLayoutStyle == YBWaterFlowHorizontalEqualHeight){
  173. attrs.frame = [self itemFrameOfHorizontalWaterFlow:indexPath];
  174. }else if(self.flowLayoutStyle == YBWaterFlowVerticalEqualHeight){
  175. attrs.frame = [self itemFrameOfVHWaterFlow:indexPath];
  176. }else if(self.flowLayoutStyle == YBLineWaterFlow){
  177. attrs.frame = [self itemFrameOfLineWaterFlow:indexPath];
  178. // 计算中心点距离
  179. CGFloat delta = fabs((attrs.center.x - self.collectionView.contentOffset.x) - self.collectionView.frame.size.width * 0.5);
  180. //计算比例
  181. CGFloat scale = 1 - delta / (self.collectionView.frame.size.width * 0.5) * 0.25;
  182. attrs.transform = CGAffineTransformMakeScale(scale, scale);
  183. }else if (self.flowLayoutStyle == YBWaterFlowHorizontalGrid){
  184. attrs.frame = [self itemFrameOfHorizontalGridWaterFlow:indexPath];
  185. }
  186. return attrs;
  187. }
  188. /** 返回indexPath位置头和脚视图对应的布局属性*/
  189. - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
  190. UICollectionViewLayoutAttributes *attri;
  191. if ([UICollectionElementKindSectionHeader isEqualToString:elementKind]) {
  192. //头视图
  193. attri = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:indexPath];
  194. attri.frame = [self headerViewFrameOfVerticalWaterFlow:indexPath];
  195. }else {
  196. //脚视图
  197. attri = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:indexPath];
  198. attri.frame = [self footerViewFrameOfVerticalWaterFlow:indexPath];
  199. }
  200. return attri;
  201. }
  202. /** 返回值决定了collectionView停止滚动时的偏移量 手指松开后执行
  203. * proposedContentOffset:原本情况下,collectionView停止滚动时最终的偏移量
  204. * velocity 滚动速率,通过这个参数可以了解滚动的方向
  205. */
  206. /*
  207. - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
  208. {
  209. if (self.flowLayoutStyle == YBLineWaterFlow) {
  210. // 拖动比较快 最终偏移量 不等于 手指离开时偏移量
  211. CGFloat collectionW = self.collectionView.frame.size.width;
  212. // 最终偏移量
  213. CGPoint targetP = [super targetContentOffsetForProposedContentOffset:proposedContentOffset withScrollingVelocity:velocity];
  214. // 0.获取最终显示的区域
  215. CGRect targetRect = CGRectMake(targetP.x, 0, collectionW, MAXFLOAT);
  216. // 1.获取最终显示的cell
  217. NSArray *attrs = [super layoutAttributesForElementsInRect:targetRect];
  218. // 获取最小间距
  219. CGFloat minDelta = MAXFLOAT;
  220. for (UICollectionViewLayoutAttributes *attr in attrs) {
  221. // 获取距离中心点距离:注意:应该用最终的x
  222. CGFloat delta = (attr.center.x - targetP.x) - self.collectionView.bounds.size.width * 0.5;
  223. if (fabs(delta) < fabs(minDelta)) {
  224. minDelta = delta;
  225. }
  226. }
  227. // 移动间距
  228. targetP.x += minDelta;
  229. if (targetP.x < 0) {
  230. targetP.x = 0;
  231. }
  232. return targetP;
  233. }
  234. return proposedContentOffset;
  235. }
  236. // Invalidate:刷新
  237. // 在滚动的时候是否允许刷新布局
  238. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
  239. if (self.flowLayoutStyle == YBLineWaterFlow) {
  240. return YES;
  241. }
  242. return NO;
  243. }
  244. */
  245. //返回内容高度
  246. -(CGSize)collectionViewContentSize {
  247. if (self.flowLayoutStyle == YBWaterFlowVerticalEqualWidth) {
  248. return CGSizeMake(0, self.maxColumnHeight + self.edgeInsets.bottom);
  249. }else if (self.flowLayoutStyle == YBWaterFlowHorizontalEqualHeight){
  250. return CGSizeMake(self.maxRowWidth + self.edgeInsets.right, 0);
  251. }else if(self.flowLayoutStyle == YBWaterFlowVerticalEqualHeight){
  252. return CGSizeMake(0 , self.maxColumnHeight + self.edgeInsets.bottom);
  253. }else if(self.flowLayoutStyle == YBLineWaterFlow){
  254. return CGSizeMake(self.maxRowWidth + self.edgeInsets.right , 0);
  255. }else if(self.flowLayoutStyle == YBWaterFlowHorizontalGrid){
  256. return CGSizeMake(self.maxRowWidth + self.edgeInsets.right,self.collectionView.frame.size.height);
  257. }
  258. return CGSizeMake(0, 0);
  259. }
  260. #pragma mark - Help Methods
  261. //竖向瀑布流 item等宽不等高
  262. - (CGRect)itemFrameOfVerticalWaterFlow:(NSIndexPath *)indexPath{
  263. //collectionView的宽度
  264. CGFloat collectionW = self.collectionView.frame.size.width;
  265. //设置布局属性item的frame
  266. CGFloat w = (collectionW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
  267. CGFloat h = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].height;
  268. //找出高度最短的那一列
  269. NSInteger destColumn = 0;
  270. CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
  271. for (NSInteger i = 1; i < self.columnCount; i++) {
  272. //取出第i列
  273. CGFloat columnHeight = [self.columnHeights[i] doubleValue];
  274. if (minColumnHeight > columnHeight) {
  275. minColumnHeight = columnHeight;
  276. destColumn = i;
  277. }
  278. }
  279. CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
  280. CGFloat y = minColumnHeight;
  281. if (y != self.edgeInsets.top) {
  282. y += self.rowMargin;
  283. }
  284. //更新最短那列的高度
  285. self.columnHeights[destColumn] = @(CGRectGetMaxY(CGRectMake(x, y, w, h)));
  286. //记录内容的高度
  287. CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
  288. if (self.maxColumnHeight < columnHeight) {
  289. self.maxColumnHeight = columnHeight;
  290. }
  291. return CGRectMake(x, y, w, h);
  292. }
  293. //竖向瀑布流 item等高不等宽
  294. - (CGRect)itemFrameOfVHWaterFlow:(NSIndexPath *)indexPath{
  295. //collectionView的宽度
  296. CGFloat collectionW = self.collectionView.frame.size.width;
  297. CGSize headViewSize = CGSizeMake(0, 0);
  298. if([self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForHeaderViewInSection:)]){
  299. headViewSize = [self.delegate waterFlowLayout:self sizeForHeaderViewInSection:indexPath.section];
  300. }
  301. CGFloat w = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].width;
  302. CGFloat h = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].height;
  303. CGFloat x;
  304. CGFloat y;
  305. //记录最后一行的内容的横坐标和纵坐标
  306. if (collectionW - [[self.rowWidths firstObject] floatValue] > w + self.edgeInsets.right) {
  307. x = [[self.rowWidths firstObject] floatValue] == self.edgeInsets.left ? self.edgeInsets.left : [[self.rowWidths firstObject] floatValue] + self.columnMargin;
  308. if ([[self.columnHeights firstObject] floatValue] == self.edgeInsets.top) {
  309. y = self.edgeInsets.top;
  310. }else if ([[self.columnHeights firstObject] floatValue] == self.edgeInsets.top + headViewSize.height) {
  311. y = self.edgeInsets.top + headViewSize.height + self.rowMargin;
  312. }else{
  313. y = [[self.columnHeights firstObject] floatValue] - h;
  314. }
  315. [self.rowWidths replaceObjectAtIndex:0 withObject:@(x + w )];
  316. if ([[self.columnHeights firstObject] floatValue] == self.edgeInsets.top || [[self.columnHeights firstObject] floatValue] == self.edgeInsets.top + headViewSize.height) {
  317. [self.columnHeights replaceObjectAtIndex:0 withObject:@(y + h)];
  318. }
  319. }else if(collectionW - [[self.rowWidths firstObject] floatValue] == w + self.edgeInsets.right){
  320. //换行
  321. x = self.edgeInsets.left;
  322. y = [[self.columnHeights firstObject] floatValue] + self.rowMargin;
  323. [self.rowWidths replaceObjectAtIndex:0 withObject:@(x + w)];
  324. [self.columnHeights replaceObjectAtIndex:0 withObject:@(y + h)];
  325. }else{
  326. //换行
  327. x = self.edgeInsets.left;
  328. y = [[self.columnHeights firstObject] floatValue] + self.rowMargin;
  329. [self.rowWidths replaceObjectAtIndex:0 withObject:@(x + w)];
  330. [self.columnHeights replaceObjectAtIndex:0 withObject:@(y + h)];
  331. }
  332. //记录内容的高度
  333. self.maxColumnHeight = [[self.columnHeights firstObject] floatValue] ;
  334. return CGRectMake(x, y, w, h);
  335. }
  336. //水平瀑布流 item等高不等宽
  337. - (CGRect)itemFrameOfHorizontalWaterFlow:(NSIndexPath *)indexPath{
  338. //collectionView的高度
  339. CGFloat collectionH = self.collectionView.frame.size.height;
  340. //设置布局属性item的frame
  341. CGFloat h = (collectionH - self.edgeInsets.top - self.edgeInsets.bottom - (self.rowCount - 1) * self.rowMargin) / self.rowCount;
  342. CGFloat w = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].width;
  343. //找出宽度最短的那一行
  344. NSInteger destRow = 0;
  345. CGFloat minRowWidth = [self.rowWidths[0] doubleValue];
  346. for (NSInteger i = 1; i < self.rowWidths.count; i++) {
  347. //取出第i行
  348. CGFloat rowWidth = [self.rowWidths[i] doubleValue];
  349. if (minRowWidth > rowWidth) {
  350. minRowWidth = rowWidth;
  351. destRow = i;
  352. }
  353. }
  354. CGFloat y = self.edgeInsets.top + destRow * (h + self.rowMargin);
  355. CGFloat x = minRowWidth;
  356. if (x != self.edgeInsets.left) {
  357. x += self.columnMargin;
  358. }
  359. //更新最短那行的宽度
  360. self.rowWidths[destRow] = @(CGRectGetMaxX(CGRectMake(x, y, w, h)));
  361. //记录内容的宽度
  362. CGFloat rowWidth = [self.rowWidths[destRow] doubleValue];
  363. if (self.maxRowWidth < rowWidth) {
  364. self.maxRowWidth = rowWidth ;
  365. }
  366. return CGRectMake(x, y, w, h);
  367. }
  368. //水平栅格布局
  369. - (CGRect)itemFrameOfHorizontalGridWaterFlow:(NSIndexPath *)indexPath{
  370. //collectionView的高度
  371. CGFloat collectionH = self.collectionView.frame.size.height;
  372. //设置布局属性item的frame
  373. CGFloat h = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].height;
  374. CGFloat w = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].width;
  375. CGFloat x = 0;
  376. CGFloat y = 0;
  377. //找出宽度最短的那一行
  378. NSInteger destRow = 0;
  379. CGFloat minRowWidth = [self.rowWidths[destRow] doubleValue];
  380. for (NSInteger i = 1; i < self.rowWidths.count; i++) {
  381. //取出第i行
  382. CGFloat rowWidth = [self.rowWidths[i] doubleValue];
  383. if (minRowWidth > rowWidth) {
  384. minRowWidth = rowWidth;
  385. destRow = i;
  386. }
  387. }
  388. y = destRow == 0 ? self.edgeInsets.top : self.edgeInsets.top + h + self.rowMargin;
  389. x = [self.rowWidths[destRow] doubleValue] == self.edgeInsets.left ? self.edgeInsets.left : [self.rowWidths[destRow] doubleValue] + self.columnMargin;
  390. //更新最短那行的宽度
  391. if (h >= collectionH - self.edgeInsets.bottom - self.edgeInsets.top) {
  392. x = [self.rowWidths[destRow] doubleValue] == self.edgeInsets.left ? self.edgeInsets.left : self.maxRowWidth + self.columnMargin;
  393. for (NSInteger i = 0; i < 2; i++) {
  394. self.rowWidths[i] = @(x + w);
  395. }
  396. }else{
  397. self.rowWidths[destRow] = @(x + w);
  398. }
  399. //记录最大宽度
  400. if (self.maxRowWidth < x + w) {
  401. self.maxRowWidth = x + w ;
  402. }
  403. return CGRectMake(x, y, w, h);
  404. }
  405. - (CGRect)itemFrameOfLineWaterFlow:(NSIndexPath *)indexPath{
  406. //collectionView的高度
  407. CGFloat collectionH = self.collectionView.frame.size.height;
  408. //设置布局属性item的frame
  409. CGFloat h = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].height;
  410. CGFloat w = [self.delegate waterFlowLayout:self sizeForItemAtIndexPath:indexPath].width;
  411. CGFloat y = self.edgeInsets.top;
  412. CGFloat x = [[self.rowWidths firstObject] floatValue];
  413. if (x != self.edgeInsets.left) {
  414. x += self.columnMargin;
  415. }
  416. //更新内容的宽度
  417. [self.rowWidths replaceObjectAtIndex:0 withObject:@(x + w)];
  418. //记录内容的宽度
  419. self.maxRowWidth = [[self.rowWidths firstObject] floatValue];
  420. return CGRectMake(x, y, w, h);
  421. }
  422. //返回头视图的布局frame
  423. - (CGRect)headerViewFrameOfVerticalWaterFlow:(NSIndexPath *)indexPath{
  424. CGSize size = CGSizeZero;
  425. if([self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForHeaderViewInSection:)]){
  426. size = [self.delegate waterFlowLayout:self sizeForHeaderViewInSection:indexPath.section];
  427. }
  428. if (self.flowLayoutStyle == YBWaterFlowVerticalEqualWidth) {
  429. CGFloat x = 0;
  430. CGFloat y = self.maxColumnHeight == 0 ? self.edgeInsets.top : self.maxColumnHeight;
  431. if (![self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForFooterViewInSection:)] || [self.delegate waterFlowLayout:self sizeForFooterViewInSection:indexPath.section].height == 0) {
  432. y = self.maxColumnHeight == 0 ? self.edgeInsets.top : self.maxColumnHeight + self.rowMargin;
  433. }
  434. y = 0;
  435. self.maxColumnHeight = y + size.height ;
  436. [self.columnHeights removeAllObjects];
  437. for (NSInteger i = 0; i < self.columnCount; i++) {
  438. [self.columnHeights addObject:@(self.maxColumnHeight)];
  439. }
  440. return CGRectMake(x , y, self.collectionView.frame.size.width, size.height);
  441. }else if (self.flowLayoutStyle == YBWaterFlowVerticalEqualHeight){
  442. CGFloat x = 0;
  443. CGFloat y = self.maxColumnHeight == 0 ? self.edgeInsets.top : self.maxColumnHeight;
  444. if (![self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForFooterViewInSection:)] || [self.delegate waterFlowLayout:self sizeForFooterViewInSection:indexPath.section].height == 0) {
  445. y = self.maxColumnHeight == 0 ? self.edgeInsets.top : self.maxColumnHeight + self.rowMargin;
  446. }
  447. self.maxColumnHeight = y + size.height ;
  448. [self.rowWidths replaceObjectAtIndex:0 withObject:@(self.collectionView.frame.size.width)];
  449. [self.columnHeights replaceObjectAtIndex:0 withObject:@(self.maxColumnHeight)];
  450. return CGRectMake(x , y, self.collectionView.frame.size.width, size.height);
  451. }else if (self.flowLayoutStyle == YBWaterFlowHorizontalEqualHeight){
  452. }
  453. return CGRectMake(0, 0, 0, 0);
  454. }
  455. //返回脚视图的布局frame
  456. - (CGRect)footerViewFrameOfVerticalWaterFlow:(NSIndexPath *)indexPath{
  457. CGSize size = CGSizeZero;
  458. if([self.delegate respondsToSelector:@selector(waterFlowLayout:sizeForFooterViewInSection:)]){
  459. size = [self.delegate waterFlowLayout:self sizeForFooterViewInSection:indexPath.section];
  460. }
  461. if (self.flowLayoutStyle == YBWaterFlowVerticalEqualWidth ) {
  462. CGFloat x = 0;
  463. CGFloat y = size.height == 0 ? self.maxColumnHeight : self.maxColumnHeight + self.rowMargin;
  464. self.maxColumnHeight = y + size.height;
  465. [self.columnHeights removeAllObjects];
  466. for (NSInteger i = 0; i < self.columnCount; i++) {
  467. [self.columnHeights addObject:@(self.maxColumnHeight)];
  468. }
  469. return CGRectMake(x , y, self.collectionView.frame.size.width, size.height);
  470. }else if (self.flowLayoutStyle == YBWaterFlowVerticalEqualHeight){
  471. CGFloat x = 0;
  472. CGFloat y = size.height == 0 ? self.maxColumnHeight : self.maxColumnHeight + self.rowMargin;
  473. self.maxColumnHeight = y + size.height;
  474. [self.rowWidths replaceObjectAtIndex:0 withObject:@(self.collectionView.frame.size.width)];
  475. [self.columnHeights replaceObjectAtIndex:0 withObject:@(self.maxColumnHeight)];
  476. return CGRectMake(x , y, self.collectionView.frame.size.width, size.height);
  477. }else if (self.flowLayoutStyle == YBWaterFlowHorizontalEqualHeight){
  478. }
  479. return CGRectMake(0, 0, 0, 0);
  480. }
  481. @end