WFCUConversationSettingMemberCollectionViewLayout.m 3.4 KB

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