CGXVerticalMenuRoundFlowLayoutUtils.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // CGXVerticalMenuRoundFlowLayoutUtils.m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuRoundFlowLayoutUtils.h"
  9. @class CGXVerticalMenuRoundFlowLayoutDelegate;
  10. @implementation CGXVerticalMenuRoundFlowLayoutUtils
  11. /// 获取cell间距
  12. /// @param sectionIndex sectionIndex description
  13. + (CGFloat)evaluatedMinimumInteritemSpacingForSectionWithCollectionLayout:(UICollectionViewFlowLayout *)layout atIndex:(NSInteger)sectionIndex {
  14. CGFloat minimumInteritemSpacing = layout.minimumInteritemSpacing;
  15. if ([layout.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
  16. // id<CGXVerticalMenuRoundFlowLayoutDelegate> delegate = (id<CGXVerticalMenuRoundFlowLayoutDelegate>)layout.collectionView.delegate;
  17. id delegate = layout.collectionView.delegate;
  18. minimumInteritemSpacing = [delegate collectionView:layout.collectionView layout:layout minimumInteritemSpacingForSectionAtIndex:sectionIndex];
  19. }
  20. return minimumInteritemSpacing;
  21. }
  22. /// 获取用户设置CollectionView 对应section的 sectionInset
  23. /// @param index index description
  24. + (UIEdgeInsets)evaluatedSectionInsetForItemWithCollectionLayout:(UICollectionViewFlowLayout *)layout atIndex:(NSInteger)index{
  25. UIEdgeInsets sectionInset = layout.sectionInset;
  26. if ([layout.collectionView.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
  27. // id<CGXVerticalMenuRoundFlowLayoutDelegate> delegate = (id<CGXVerticalMenuRoundFlowLayoutDelegate>)layout.collectionView.delegate;
  28. id delegate = layout.collectionView.delegate;
  29. sectionInset = [delegate collectionView:layout.collectionView layout:layout insetForSectionAtIndex:index];
  30. }
  31. return sectionInset;
  32. }
  33. #pragma mark - 不规则Cell计算方案
  34. /// 不规则cell找出top最高位置
  35. /// @param section section description
  36. /// @param numberOfItems numberOfItems description
  37. /// @param defaultFrame defaultFrame description
  38. + (CGRect)calculateIrregularitiesCellByMinTopFrameWithLayout:(UICollectionViewFlowLayout *)layout section:(NSInteger)section numberOfItems:(NSInteger)numberOfItems defaultFrame:(CGRect)defaultFrame{
  39. CGRect firstFrame = defaultFrame;
  40. if (layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
  41. //竖向
  42. CGFloat minY = CGRectGetMinY(firstFrame);
  43. for (NSInteger i = 0; i <= numberOfItems - 1; i ++ ) {
  44. UICollectionViewLayoutAttributes *attr = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]];
  45. minY = MIN(minY, CGRectGetMinY(attr.frame));
  46. }
  47. CGRect rect = firstFrame;
  48. firstFrame = CGRectMake(rect.origin.x, minY, rect.size.width, rect.size.height);
  49. }else{
  50. //横向
  51. CGFloat minX = CGRectGetMinX(firstFrame);
  52. for (NSInteger i = 0; i <= numberOfItems - 1; i ++ ) {
  53. UICollectionViewLayoutAttributes *attr = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]];
  54. minX = MIN(minX, CGRectGetMinX(attr.frame));
  55. }
  56. CGRect rect = firstFrame;
  57. firstFrame = CGRectMake(minX ,rect.origin.y, rect.size.width, rect.size.height);
  58. }
  59. return firstFrame;
  60. }
  61. /// 不规则cell找出bootom最低位置
  62. /// @param layout layout description
  63. /// @param section section description
  64. /// @param numberOfItems numberOfItems description
  65. /// @param defaultFrame defaultFrame description
  66. + (CGRect)calculateIrregularitiesCellByMaxBottomFrameWithLayout:(UICollectionViewFlowLayout *)layout section:(NSInteger)section numberOfItems:(NSInteger)numberOfItems defaultFrame:(CGRect)defaultFrame{
  67. CGRect lastFrame = defaultFrame;
  68. if (layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
  69. //竖向
  70. CGFloat maxY = CGRectGetMinY(lastFrame);
  71. NSInteger index = numberOfItems-1;
  72. for (NSInteger i = 0; i <= numberOfItems - 1; i ++ ) {
  73. UICollectionViewLayoutAttributes *attr = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]];
  74. if (maxY < MAX(maxY, CGRectGetMaxY(attr.frame))) {
  75. maxY = MAX(maxY, CGRectGetMaxY(attr.frame));
  76. index = i;
  77. }
  78. }
  79. lastFrame = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:section]].frame;
  80. }else{
  81. //横向
  82. CGFloat maxX = CGRectGetMaxX(lastFrame);
  83. NSInteger index = numberOfItems-1;
  84. for (NSInteger i = 0; i <= numberOfItems - 1; i ++ ) {
  85. UICollectionViewLayoutAttributes *attr = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:section]];
  86. if (maxX < MAX(maxX, CGRectGetMaxX(attr.frame))) {
  87. maxX = MAX(maxX, CGRectGetMaxX(attr.frame));
  88. index = i;
  89. }
  90. }
  91. lastFrame = [layout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:section]].frame;
  92. }
  93. return lastFrame;
  94. }
  95. @end