TeacherStyleFlowLayout.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // TeacherStyleFlowLayout.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2022/8/30.
  6. //
  7. #import "TeacherStyleFlowLayout.h"
  8. @interface TeacherStyleFlowLayout ()
  9. /** 这个字典用来存储每一列item的高度 */
  10. @property (strong,nonatomic)NSMutableDictionary *maxYDic;
  11. /** 存放每一个item的布局属性 */
  12. @property (strong,nonatomic)NSMutableArray *attrsArray;
  13. @end
  14. @implementation TeacherStyleFlowLayout
  15. - (instancetype)init {
  16. if (self = [super init]){
  17. self.columnMargin = 12;
  18. self.rowMargin = 11;
  19. self.columnCount = 2;
  20. }
  21. return self;
  22. }
  23. //每一次布局前的准备工作
  24. -(void)prepareLayout
  25. {
  26. [super prepareLayout];
  27. //清空最大的y值
  28. for (int i =0; i < self.columnCount; i++)
  29. {
  30. NSString *column = [NSString stringWithFormat:@"%d",i];
  31. self.maxYDic[column] = @(self.sectionInset.top);
  32. }
  33. //计算所有item的属性
  34. [self.attrsArray removeAllObjects];
  35. NSInteger count = [self.collectionView numberOfItemsInSection:0];
  36. for (int i=0; i<count; i++)
  37. {
  38. UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
  39. [self.attrsArray addObject:attrs];
  40. }
  41. }
  42. //设置collectionView滚动区域
  43. - (CGSize)collectionViewContentSize
  44. {
  45. //假设最长的那一列为第0列
  46. __block NSString *maxColumn = @"0";
  47. //遍历字典,找出最长的那一列
  48. [self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
  49. if ([maxY floatValue] > [self.maxYDic[maxColumn] floatValue])
  50. {
  51. maxColumn = column;
  52. }
  53. }];
  54. return CGSizeMake(0, [self.maxYDic[maxColumn]floatValue]+self.sectionInset.bottom);
  55. }
  56. //允许每一次重新布局
  57. -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  58. {
  59. return YES;
  60. }
  61. //布局每一个属性
  62. -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
  63. {
  64. //假设最短的那一列为第0列
  65. __block NSString *minColumn = @"0";
  66. //遍历字典,找出最短的那一列
  67. [self.maxYDic enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
  68. if ([maxY floatValue] < [self.maxYDic[minColumn] floatValue])
  69. {
  70. minColumn = column;
  71. }
  72. }];
  73. //计算每一个item的宽度和高度
  74. CGFloat width = (self.collectionView.frame.size.width - self.columnMargin*(self.columnCount - 1) - self.sectionInset.left - self.sectionInset.right) / self.columnCount;
  75. CGFloat height = [self.delegate waterFlowLayout:self heightForWidth:width andIndexPath:indexPath] ;
  76. //计算每一个item的位置
  77. CGFloat x = self.sectionInset.left + (width + self.columnMargin) * [minColumn floatValue];
  78. CGFloat y = [self.maxYDic[minColumn] floatValue] + self.rowMargin;
  79. //更新这一列的y值
  80. self.maxYDic[minColumn] = @(y + height);
  81. //创建布局属性
  82. UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  83. //设置item的frame
  84. attrs.frame = CGRectMake(x, y, width, height);
  85. return attrs;
  86. }
  87. //布局所有item的属性,包括header、footer
  88. -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  89. {
  90. return self.attrsArray;
  91. }
  92. #pragma mark ---- lazying
  93. - (NSMutableDictionary *)maxYDic {
  94. if (!_maxYDic) {
  95. _maxYDic = [NSMutableDictionary dictionary];
  96. }
  97. return _maxYDic;
  98. }
  99. - (NSMutableArray *)attrsArray {
  100. if (!_attrsArray) {
  101. _attrsArray = [NSMutableArray array];
  102. }
  103. return _attrsArray;
  104. }
  105. @end