TUIMenuView.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // MenuView.m
  3. // UIKit
  4. //
  5. // Created by kennethmiao on 2018/9/18.
  6. // Copyright © 2018 Tencent. All rights reserved.
  7. //
  8. #import "TUIMenuView.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import <TUICore/TUIDarkModel.h>
  11. #import <TUICore/TUIGlobalization.h>
  12. #import <TUICore/TUIThemeManager.h>
  13. #import "TUIMenuCell.h"
  14. @interface TUIMenuView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  15. @property(nonatomic, strong) NSMutableArray<TUIMenuCellData *> *data;
  16. @end
  17. @implementation TUIMenuView
  18. - (id)initWithFrame:(CGRect)frame {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. [self setupViews];
  22. }
  23. return self;
  24. }
  25. - (void)setData:(NSMutableArray<TUIMenuCellData *> *)data {
  26. _data = data;
  27. [_menuCollectionView reloadData];
  28. [self defaultLayout];
  29. [_menuCollectionView layoutIfNeeded];
  30. [_menuCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  31. }
  32. - (void)setupViews {
  33. self.backgroundColor = TUIChatDynamicColor(@"chat_input_controller_bg_color", @"#EBF0F6");
  34. _menuFlowLayout = [[TUICollectionRTLFitFlowLayout alloc] init];
  35. _menuFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  36. _menuFlowLayout.minimumLineSpacing = 0;
  37. _menuFlowLayout.minimumInteritemSpacing = 0;
  38. //_menuFlowLayout.headerReferenceSize = CGSizeMake(TMenuView_Margin, 1);
  39. _menuCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_menuFlowLayout];
  40. [_menuCollectionView registerClass:[TUIMenuCell class] forCellWithReuseIdentifier:TMenuCell_ReuseId];
  41. [_menuCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:TMenuCell_Line_ReuseId];
  42. _menuCollectionView.collectionViewLayout = _menuFlowLayout;
  43. _menuCollectionView.delegate = self;
  44. _menuCollectionView.dataSource = self;
  45. _menuCollectionView.showsHorizontalScrollIndicator = NO;
  46. _menuCollectionView.showsVerticalScrollIndicator = NO;
  47. _menuCollectionView.backgroundColor = self.backgroundColor;
  48. _menuCollectionView.alwaysBounceHorizontal = YES;
  49. _menuCollectionView.backgroundColor = TUIChatDynamicColor(@"chat_input_controller_bg_color", @"#EBF0F6");
  50. [self addSubview:_menuCollectionView];
  51. }
  52. - (void)defaultLayout {
  53. [_menuCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
  54. make.leading.mas_equalTo(0);
  55. make.trailing.mas_equalTo(self.mas_trailing).mas_offset(0);
  56. make.height.mas_equalTo(40);
  57. make.centerY.mas_equalTo(self);
  58. }];
  59. }
  60. - (void)sendUpInside:(UIButton *)sender {
  61. if (_delegate && [_delegate respondsToSelector:@selector(menuViewDidSendMessage:)]) {
  62. [_delegate menuViewDidSendMessage:self];
  63. }
  64. }
  65. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  66. return _data.count * 2;
  67. }
  68. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  69. if (indexPath.row % 2 == 0) {
  70. TUIMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TMenuCell_ReuseId forIndexPath:indexPath];
  71. [cell setData:_data[indexPath.row / 2]];
  72. return cell;
  73. } else {
  74. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TMenuCell_Line_ReuseId forIndexPath:indexPath];
  75. cell.backgroundColor = [UIColor clearColor];
  76. return cell;
  77. }
  78. }
  79. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  80. if (indexPath.row % 2 != 0) {
  81. return;
  82. }
  83. for (NSInteger i = 0; i < _data.count; ++i) {
  84. TUIMenuCellData *data = _data[i];
  85. data.isSelected = (i == indexPath.row / 2);
  86. }
  87. [_menuCollectionView reloadData];
  88. if (_delegate && [_delegate respondsToSelector:@selector(menuView:didSelectItemAtIndex:)]) {
  89. [_delegate menuView:self didSelectItemAtIndex:indexPath.row / 2];
  90. }
  91. }
  92. - (CGSize)collectionView:(UICollectionView *)collectionView
  93. layout:(UICollectionViewLayout *)collectionViewLayout
  94. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  95. if (indexPath.row % 2 == 0) {
  96. CGFloat wh = collectionView.frame.size.height;
  97. return CGSizeMake(wh, wh);
  98. } else {
  99. return CGSizeMake(TLine_Heigh, collectionView.frame.size.height);
  100. }
  101. }
  102. - (void)scrollToMenuIndex:(NSInteger)index {
  103. for (NSInteger i = 0; i < _data.count; ++i) {
  104. TUIMenuCellData *data = _data[i];
  105. data.isSelected = (i == index);
  106. }
  107. [_menuCollectionView reloadData];
  108. }
  109. @end