WFCUConferenceCollectionViewLayout.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // WFCUConferenceCollectionViewLayout.m
  3. // WFChatUIKit
  4. //
  5. // Created by Rain on 2022/9/21.
  6. // Copyright © 2022 Wildfirechat. All rights reserved.
  7. //
  8. #import "WFCUConferenceCollectionViewLayout.h"
  9. @interface WFCUConferenceCollectionViewLayout ()
  10. @property (strong, nonatomic) NSMutableArray * attrubutesArray;
  11. @end
  12. @implementation WFCUConferenceCollectionViewLayout
  13. - (void)prepareLayout {
  14. [super prepareLayout];
  15. NSInteger count = [self.collectionView numberOfItemsInSection:0];
  16. self.attrubutesArray = [NSMutableArray array];
  17. CGRect rect = CGRectZero;
  18. rect.size = self.collectionView.bounds.size;
  19. if(self.audioOnly) {
  20. for (int i = 0; i < count; i ++) {
  21. NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  22. UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  23. attributes.frame = CGRectMake(i * rect.size.width, 0, rect.size.width, rect.size.height);
  24. [self.attrubutesArray addObject:attributes];
  25. }
  26. } else {
  27. CGFloat width = rect.size.width/2;
  28. CGFloat height = rect.size.height/2;
  29. int lastPageCount = (count - 1)%4;
  30. int lastPageFirstIndex = (int)count - lastPageCount;
  31. for (int i = 0; i < count; i ++) {
  32. NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  33. UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  34. CGRect frame;
  35. if(i == 0) {
  36. frame = rect;
  37. } else {
  38. int page = [self pageByRow:i];
  39. CGFloat startX = page * rect.size.width;
  40. int index = (i -1)%4;
  41. CGFloat x = startX;
  42. CGFloat y = 0;
  43. if(i < lastPageFirstIndex) {
  44. if(index == 1 || index == 3) {
  45. x += width;
  46. }
  47. if(index == 2 || index == 3) {
  48. y += height;
  49. }
  50. } else {
  51. if(lastPageCount == 1) {
  52. x += width/2;
  53. y += height/2;
  54. } else if(lastPageCount == 2) {
  55. x += index * width;
  56. y += height/2;
  57. } else {
  58. if(index == 2) {
  59. x += width/2;
  60. y += height;
  61. } else if(index == 1) {
  62. x += width;
  63. }
  64. }
  65. }
  66. frame = CGRectMake(x , y, width, height);
  67. }
  68. attributes.frame = frame;
  69. [self.attrubutesArray addObject:attributes];
  70. }
  71. }
  72. }
  73. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  74. return self.attrubutesArray[indexPath.row];
  75. }
  76. - (CGSize)collectionViewContentSize {
  77. if(self.audioOnly) {
  78. return CGSizeMake(self.attrubutesArray.count * self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
  79. } else {
  80. if(self.attrubutesArray.count == 1 || self.attrubutesArray.count == 2) {
  81. return self.collectionView.bounds.size;
  82. }
  83. int page = [self pageByRow:self.attrubutesArray.count - 1];
  84. return CGSizeMake((page + 1) * self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
  85. }
  86. }
  87. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
  88. return _attrubutesArray;
  89. }
  90. - (int)pageByRow:(int)row {
  91. if(self.audioOnly) {
  92. return (row-1)/12;
  93. }
  94. if(row == 0){
  95. return 0;
  96. }
  97. return (row -1)/4 + 1;
  98. }
  99. - (NSMutableArray<NSIndexPath *> *)itemsInPage:(int)page {
  100. NSInteger count = [self.collectionView numberOfItemsInSection:0];
  101. NSMutableArray *arr = [[NSMutableArray alloc] init];
  102. if(page == 0) {
  103. [arr addObject:[NSIndexPath indexPathForRow:0 inSection:0]];
  104. } else {
  105. for (int i = (page-1)*4 +1; i <= MIN(page*4, count-1); i++) {
  106. [arr addObject:[NSIndexPath indexPathForRow:i inSection:0]];
  107. }
  108. }
  109. return arr;
  110. }
  111. - (CGPoint)getOffsetOfItems:(NSArray<NSIndexPath *> *)items {
  112. if(!items.count) {
  113. return CGPointMake(0, 0);
  114. }
  115. int minRow = 0x1FFFFFFF;
  116. int maxRow = 0;
  117. for (NSIndexPath *indexPath in items) {
  118. if(indexPath.row < minRow) {
  119. minRow = (int)indexPath.row;
  120. }
  121. if(indexPath.row > maxRow) {
  122. maxRow = (int)indexPath.row;
  123. }
  124. }
  125. CGFloat width = self.collectionView.bounds.size.width;
  126. float start;
  127. int pageRight = [self pageByRow:maxRow];
  128. float end = pageRight * width;
  129. int pageLeft = 0;
  130. if(minRow == 0) {
  131. start = 0;
  132. } else {
  133. pageLeft = [self pageByRow:minRow];
  134. start = pageLeft * width;
  135. }
  136. return CGPointMake(start, end);
  137. }
  138. @end