WFCUConversationSettingMemberCollectionViewLayout.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // ConversationSettingMemberCollectionViewLayout.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/11/3.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUConversationSettingMemberCollectionViewLayout.h"
  9. @interface WFCUConversationSettingMemberCollectionViewLayout()
  10. @property(nonatomic, strong) NSMutableArray *attributesArray;
  11. @property(nonatomic, assign) CGFloat itemAreaWidth;
  12. @property(nonatomic, assign) CGFloat itemWidth;
  13. @property(nonatomic, assign) CGFloat itemMargin;
  14. @property(nonatomic, assign) int itemsPerLine;
  15. @end
  16. @implementation WFCUConversationSettingMemberCollectionViewLayout
  17. - (instancetype)initWithItemMargin:(CGFloat)itemMargin {
  18. self = [super init];
  19. if (self) {
  20. self.itemMargin = itemMargin;
  21. self.itemsPerLine = 5;
  22. }
  23. return self;
  24. }
  25. - (CGFloat)itemAreaWidth {
  26. if (!_itemAreaWidth) {
  27. CGRect frame = [UIScreen mainScreen].bounds;
  28. _itemAreaWidth = frame.size.width / self.itemsPerLine;
  29. }
  30. return _itemAreaWidth;
  31. }
  32. - (CGFloat)itemWidth {
  33. if (!_itemWidth) {
  34. _itemWidth = self.itemAreaWidth - self.itemMargin - self.itemMargin;
  35. }
  36. return _itemWidth;
  37. }
  38. - (void)prepareLayout {
  39. int itemCount = (int)[self.collectionView numberOfItemsInSection:0];
  40. if (itemCount == 0) {
  41. [super prepareLayout];
  42. return;
  43. }
  44. self.attributesArray = [[NSMutableArray alloc] init];
  45. for (int i = 0; i < itemCount; i++) {
  46. int row = i / self.itemsPerLine;
  47. int column = i % self.itemsPerLine;
  48. UICollectionViewLayoutAttributes *attributes =
  49. [UICollectionViewLayoutAttributes
  50. layoutAttributesForCellWithIndexPath:[NSIndexPath
  51. indexPathForItem:i
  52. inSection:0]];
  53. attributes.frame = CGRectMake(column * self.itemAreaWidth + self.itemMargin,
  54. row * self.itemAreaWidth + self.itemMargin,
  55. self.itemWidth,
  56. self.itemWidth);
  57. [self.attributesArray addObject:attributes];
  58. }
  59. self.scrollDirection = UICollectionViewScrollDirectionVertical;
  60. [super prepareLayout];
  61. }
  62. - (CGFloat)getHeigthOfItemCount:(int)itemCount {
  63. if (itemCount == 0) {
  64. return 0;
  65. } else {
  66. int lines = (itemCount - 1) / 5 + 1;
  67. CGFloat height = self.itemAreaWidth * lines;
  68. height += 12;
  69. return height;
  70. }
  71. }
  72. - (CGSize)collectionViewContentSize {
  73. return CGSizeMake([self collectionView].frame.size.width, [self getHeigthOfItemCount:(int)[self.collectionView numberOfItemsInSection:0]]);
  74. }
  75. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:
  76. (NSIndexPath *)path {
  77. UICollectionViewLayoutAttributes *attributes =
  78. [self.attributesArray objectAtIndex:[path row]];
  79. return attributes;
  80. }
  81. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  82. NSMutableArray *attributes = [NSMutableArray array];
  83. for (NSInteger i = 0; i < [self.collectionView numberOfItemsInSection:0];
  84. i++) {
  85. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  86. [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
  87. }
  88. return attributes;
  89. }
  90. @end