WFCUParticipantCollectionViewLayout.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // WFCUParticipantCollectionViewLayout.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/1/21.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #if WFCU_SUPPORT_VOIP
  9. #import "WFCUParticipantCollectionViewLayout.h"
  10. @interface WFCUParticipantCollectionViewLayout ()
  11. @property (assign, nonatomic) UIEdgeInsets sectionInsets;
  12. @property (strong, nonatomic) NSMutableArray * attrubutesArray; //所有元素的布局信
  13. @end
  14. @implementation WFCUParticipantCollectionViewLayout
  15. - (void)prepareLayout {
  16. [super prepareLayout];
  17. NSInteger count = [self.collectionView numberOfItemsInSection:0];
  18. int column = 0;
  19. int line = 0;
  20. if (count == 1) {
  21. column = 1;
  22. line = 1;
  23. } else if(count <= 4) {
  24. column = 2;
  25. if (count <= 2) {
  26. line = 1;
  27. } else {
  28. line = 2;
  29. }
  30. } else {
  31. column = 3;
  32. if (count <= 6) {
  33. line = 2;
  34. } else {
  35. line = 3;
  36. }
  37. }
  38. CGRect collectionViewRect = self.collectionView.bounds;
  39. CGPoint line1Start;
  40. int line1Number = (int)count - (line - 1) * column;
  41. line1Start.x = (collectionViewRect.size.width - (line1Number * (self.itemWidth + self.itemSpace) - self.itemSpace))/2;
  42. line1Start.y = (collectionViewRect.size.height - (line *(self.itemHeight + self.lineSpace) - self.lineSpace))/2;
  43. int line2Number = line1Number + column;
  44. int line3Number = line2Number + column;
  45. CGPoint line2Start = CGPointZero;
  46. CGPoint line3Start = CGPointZero;
  47. self.attrubutesArray = [NSMutableArray array];
  48. for (NSInteger i = 0; i < count; i ++) {
  49. NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  50. UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  51. CGRect frame = CGRectMake(0, 0, self.itemWidth, self.itemHeight);
  52. if (i < line1Number) {
  53. frame.origin.x = line1Start.x + (self.itemWidth + self.itemSpace)*i;
  54. frame.origin.y = line1Start.y;
  55. } else if(i < line2Number) {
  56. if (line2Start.y == 0) {
  57. line2Start.x = (collectionViewRect.size.width - (MIN(3, count-line1Number) * (self.itemWidth + self.itemSpace) - self.itemSpace))/2;
  58. line2Start.y = line1Start.y + self.itemHeight + self.lineSpace;
  59. }
  60. frame.origin.x = line2Start.x + (self.itemWidth + self.itemSpace)*(i-line1Number);
  61. frame.origin.y = line2Start.y;
  62. } else if(i < line3Number) {
  63. if (line3Start.y == 0) {
  64. line3Start.x = line2Start.x;
  65. line3Start.y = line2Start.y + self.itemHeight + self.lineSpace;
  66. }
  67. frame.origin.x = line3Start.x + (self.itemWidth + self.itemSpace)*(i-line2Number);
  68. frame.origin.y = line3Start.y;
  69. }
  70. attributes.frame = frame;
  71. [self.attrubutesArray addObject:attributes];
  72. }
  73. }
  74. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  75. return self.attrubutesArray[indexPath.row];
  76. }
  77. - (CGSize)collectionViewContentSize {
  78. return self.collectionView.bounds.size;
  79. }
  80. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
  81. return _attrubutesArray;
  82. }
  83. @end
  84. #endif