TUIMoreView.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // TUIMoreView.m
  3. // UIKit
  4. //
  5. // Created by kennethmiao on 2018/9/21.
  6. // Copyright © 2018 Tencent. All rights reserved.
  7. //
  8. #import "TUIMoreView.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import <TUICore/TUIThemeManager.h>
  11. #import "TUIInputMoreCell.h"
  12. @interface TUIMoreView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  13. @property(nonatomic, strong) NSArray *data;
  14. @property(nonatomic, strong) NSMutableDictionary *itemIndexs;
  15. @property(nonatomic, assign) NSInteger sectionCount;
  16. @property(nonatomic, assign) NSInteger itemsInSection;
  17. @property(nonatomic, assign) NSInteger rowCount;
  18. @end
  19. @implementation TUIMoreView
  20. - (id)initWithFrame:(CGRect)frame {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. [self setupViews];
  24. [self defaultLayout];
  25. }
  26. return self;
  27. }
  28. - (void)setupViews {
  29. self.backgroundColor = TUIChatDynamicColor(@"chat_input_controller_bg_color", @"#EBF0F6");
  30. _moreFlowLayout = [[TUICollectionRTLFitFlowLayout alloc] init];
  31. _moreFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  32. _moreFlowLayout.minimumLineSpacing = 0;
  33. _moreFlowLayout.minimumInteritemSpacing = 0;
  34. _moreFlowLayout.sectionInset = UIEdgeInsetsMake(0, TMoreView_Section_Padding, 0, TMoreView_Section_Padding);
  35. _moreCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_moreFlowLayout];
  36. [_moreCollectionView registerClass:[TUIInputMoreCell class] forCellWithReuseIdentifier:TMoreCell_ReuseId];
  37. _moreCollectionView.collectionViewLayout = _moreFlowLayout;
  38. _moreCollectionView.pagingEnabled = YES;
  39. _moreCollectionView.delegate = self;
  40. _moreCollectionView.dataSource = self;
  41. _moreCollectionView.showsHorizontalScrollIndicator = NO;
  42. _moreCollectionView.showsVerticalScrollIndicator = NO;
  43. _moreCollectionView.backgroundColor = self.backgroundColor;
  44. _moreCollectionView.alwaysBounceHorizontal = YES;
  45. [self addSubview:_moreCollectionView];
  46. _lineView = [[UIView alloc] init];
  47. _lineView.backgroundColor = TIMCommonDynamicColor(@"separator_color", @"#DBDBDB");
  48. [self addSubview:_lineView];
  49. _pageControl = [[UIPageControl alloc] init];
  50. _pageControl.currentPageIndicatorTintColor = TUIChatDynamicColor(@"chat_face_page_control_current_color", @"#7D7D7D");
  51. _pageControl.pageIndicatorTintColor = TUIChatDynamicColor(@"chat_face_page_control_color", @"#DEDEDE");
  52. [self addSubview:_pageControl];
  53. }
  54. - (void)defaultLayout {
  55. CGSize cellSize = [TUIInputMoreCell getSize];
  56. CGFloat collectionHeight = cellSize.height * _rowCount + TMoreView_Margin * (_rowCount - 1);
  57. _lineView.frame = CGRectMake(0, 0, self.frame.size.width, TLine_Heigh);
  58. _moreCollectionView.frame =
  59. CGRectMake(0, _lineView.frame.origin.y + _lineView.frame.size.height + TMoreView_Margin, self.frame.size.width, collectionHeight);
  60. if (_sectionCount > 1) {
  61. _pageControl.frame =
  62. CGRectMake(0, _moreCollectionView.frame.origin.y + _moreCollectionView.frame.size.height, self.frame.size.width, TMoreView_Page_Height);
  63. _pageControl.hidden = NO;
  64. } else {
  65. _pageControl.hidden = YES;
  66. }
  67. if (_rowCount > 1) {
  68. _moreFlowLayout.minimumInteritemSpacing = (_moreCollectionView.frame.size.height - cellSize.height * _rowCount) / (_rowCount - 1);
  69. }
  70. CGFloat margin = TMoreView_Section_Padding;
  71. CGFloat spacing = (_moreCollectionView.frame.size.width - cellSize.width * TMoreView_Column_Count - 2 * margin) / (TMoreView_Column_Count - 1);
  72. _moreFlowLayout.minimumLineSpacing = spacing;
  73. _moreFlowLayout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
  74. CGFloat height = _moreCollectionView.frame.origin.y + _moreCollectionView.frame.size.height + TMoreView_Margin;
  75. if (_sectionCount > 1) {
  76. height = _pageControl.frame.origin.y + _pageControl.frame.size.height;
  77. }
  78. CGRect frame = self.frame;
  79. frame.size.height = height;
  80. self.frame = frame;
  81. }
  82. - (void)setData:(NSArray *)data {
  83. _data = data;
  84. if (_data.count > TMoreView_Column_Count) {
  85. _rowCount = 2;
  86. } else {
  87. _rowCount = 1;
  88. }
  89. _itemsInSection = TMoreView_Column_Count * _rowCount;
  90. _sectionCount = ceil(_data.count * 1.0 / _itemsInSection);
  91. _pageControl.numberOfPages = _sectionCount;
  92. _itemIndexs = [NSMutableDictionary dictionary];
  93. for (NSInteger curSection = 0; curSection < _sectionCount; ++curSection) {
  94. for (NSInteger itemIndex = 0; itemIndex < _itemsInSection; ++itemIndex) {
  95. // transpose line/row
  96. NSInteger row = itemIndex % _rowCount;
  97. NSInteger column = itemIndex / _rowCount;
  98. NSInteger reIndex = TMoreView_Column_Count * row + column + curSection * _itemsInSection;
  99. [_itemIndexs setObject:@(reIndex) forKey:[NSIndexPath indexPathForRow:itemIndex inSection:curSection]];
  100. }
  101. }
  102. [_moreCollectionView reloadData];
  103. [self defaultLayout];
  104. }
  105. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  106. return _sectionCount;
  107. }
  108. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  109. return _itemsInSection;
  110. }
  111. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  112. TUIInputMoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TMoreCell_ReuseId forIndexPath:indexPath];
  113. TUIInputMoreCellData *data;
  114. NSNumber *index = _itemIndexs[indexPath];
  115. if (index.integerValue >= _data.count) {
  116. data = nil;
  117. } else {
  118. data = _data[index.integerValue];
  119. }
  120. [cell fillWithData:data];
  121. return cell;
  122. }
  123. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  124. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  125. if (_delegate && [_delegate respondsToSelector:@selector(moreView:didSelectMoreCell:)]) {
  126. if ([cell isKindOfClass:[TUIInputMoreCell class]]) {
  127. [_delegate moreView:self didSelectMoreCell:(TUIInputMoreCell *)cell];
  128. ;
  129. }
  130. }
  131. }
  132. - (CGSize)collectionView:(UICollectionView *)collectionView
  133. layout:(UICollectionViewLayout *)collectionViewLayout
  134. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  135. return [TUIInputMoreCell getSize];
  136. }
  137. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  138. CGFloat contentOffset = scrollView.contentOffset.x;
  139. float page = contentOffset / scrollView.frame.size.width;
  140. if ((int)(page * 10) % 10 == 0) {
  141. _pageControl.currentPage = page;
  142. }
  143. }
  144. @end