EMEmoticonGroup.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // EMEmoticonGroup.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/31.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMEmoticonGroup.h"
  9. EMEmoticonGroup *gGifGroup = nil;
  10. @implementation EMEmoticonModel
  11. - (instancetype)initWithType:(EMEmotionType)aType
  12. {
  13. self = [super init];
  14. if (self) {
  15. _type = aType;
  16. }
  17. return self;
  18. }
  19. @end
  20. @implementation EMEmoticonGroup
  21. - (instancetype)initWithType:(EMEmotionType)aType
  22. dataArray:(NSArray<EMEmoticonModel *> *)aDataArray
  23. icon:(UIImage *)aIcon
  24. rowCount:(NSInteger)aRowCount
  25. colCount:(NSInteger)aColCount
  26. {
  27. self = [super init];
  28. if (self) {
  29. _type = aType;
  30. _dataArray = aDataArray;
  31. _icon = aIcon;
  32. _rowCount = aRowCount;
  33. _colCount = aColCount;
  34. }
  35. return self;
  36. }
  37. + (instancetype)getGifGroup
  38. {
  39. if (gGifGroup) {
  40. return gGifGroup;
  41. }
  42. NSMutableArray *models2 = [[NSMutableArray alloc] init];
  43. NSArray *names = @[@"icon_002",@"icon_007",@"icon_010",@"icon_012",@"icon_013",@"icon_018",@"icon_019",@"icon_020",@"icon_021",@"icon_022",@"icon_024",@"icon_027",@"icon_029",@"icon_030",@"icon_035",@"icon_040"];
  44. int index = 0;
  45. for (NSString *name in names) {
  46. ++index;
  47. EMEmoticonModel *model = [[EMEmoticonModel alloc] initWithType:EMEmotionTypeGif];
  48. model.eId = [NSString stringWithFormat:@"em%d",(1000 + index)];
  49. model.name = [NSString stringWithFormat:@"[示例%d]", index];
  50. model.imgName = [NSString stringWithFormat:@"%@_cover", name];
  51. model.original = name;
  52. [models2 addObject:model];
  53. }
  54. NSString *tagImgName = [models2[0] imgName];
  55. gGifGroup = [[EMEmoticonGroup alloc] initWithType:EMEmotionTypeGif dataArray:models2 icon:[UIImage imageNamed:tagImgName] rowCount:2 colCount:4];
  56. return gGifGroup;
  57. }
  58. @end
  59. @implementation EMEmoticonCell
  60. - (instancetype)initWithFrame:(CGRect)frame
  61. {
  62. self = [super initWithFrame:frame];
  63. if (self) {
  64. [self _setupSubviews];
  65. }
  66. return self;
  67. }
  68. #pragma mark - Subviews
  69. - (void)_setupSubviews
  70. {
  71. self.backgroundColor = [UIColor clearColor];
  72. self.imgView = [[UIImageView alloc] init];
  73. self.imgView.contentMode = UIViewContentModeScaleAspectFit;
  74. [self addSubview:self.imgView];
  75. [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.centerX.equalTo(self);
  77. make.top.equalTo(self);
  78. }];
  79. self.label = [[UILabel alloc] init];
  80. self.label.textColor = [UIColor grayColor];
  81. self.label.font = [UIFont systemFontOfSize:14];
  82. [self addSubview:self.label];
  83. [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.top.equalTo(self.imgView.mas_bottom).offset(5);
  85. make.centerX.equalTo(self);
  86. make.bottom.equalTo(self);
  87. make.height.greaterThanOrEqualTo(@14);
  88. }];
  89. }
  90. #pragma mark - Setter
  91. - (void)setModel:(EMEmoticonModel *)model
  92. {
  93. _model = model;
  94. if (model.type == EMEmotionTypeEmoji) {
  95. self.label.font = [UIFont fontWithName:@"AppleColorEmoji" size:29.0];
  96. }
  97. self.label.text = model.name;
  98. if ([model.imgName length] > 0) {
  99. self.imgView.image = [UIImage imageNamed:model.imgName];
  100. }
  101. }
  102. @end
  103. @interface EMEmoticonView()<UICollectionViewDataSource, UICollectionViewDelegate>
  104. @property (nonatomic, strong) EMEmoticonGroup *emotionGroup;
  105. @property (nonatomic, strong) UICollectionView *collectionView;
  106. @property (nonatomic) CGSize itemSize;
  107. @property (nonatomic) CGFloat itemMargin;
  108. @end
  109. @implementation EMEmoticonView
  110. - (instancetype)initWithEmotionGroup:(EMEmoticonGroup *)aEmotionGroup
  111. {
  112. self = [super init];
  113. if (self) {
  114. _emotionGroup = aEmotionGroup;
  115. _viewHeight = 160;
  116. _itemMargin = 8;
  117. CGFloat width = ([UIScreen mainScreen].bounds.size.width - (_itemMargin * (aEmotionGroup.colCount + 1))) / aEmotionGroup.colCount;
  118. CGFloat height = (_viewHeight - (_itemMargin * (aEmotionGroup.rowCount + 1))) / aEmotionGroup.rowCount;
  119. _itemSize = CGSizeMake(width, height);
  120. [self _setupSubviews];
  121. }
  122. return self;
  123. }
  124. #pragma mark - Subviews
  125. - (void)_setupSubviews
  126. {
  127. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  128. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  129. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
  130. self.collectionView.backgroundColor = [UIColor clearColor];
  131. self.collectionView.delegate = self;
  132. self.collectionView.dataSource = self;
  133. self.collectionView.showsVerticalScrollIndicator = NO;
  134. self.collectionView.showsHorizontalScrollIndicator = NO;
  135. self.collectionView.alwaysBounceHorizontal = YES;
  136. self.collectionView.pagingEnabled = YES;
  137. // self.collectionView.userInteractionEnabled = YES;
  138. [self.collectionView registerClass:[EMEmoticonCell class] forCellWithReuseIdentifier:@"EMEmoticonCell"];
  139. [self addSubview:self.collectionView];
  140. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.edges.equalTo(self);
  142. }];
  143. }
  144. #pragma mark - UICollectionViewDataSource
  145. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  146. return 1;
  147. }
  148. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  149. return [self.emotionGroup.dataArray count];
  150. }
  151. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. EMEmoticonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EMEmoticonCell" forIndexPath:indexPath];
  154. cell.model = self.emotionGroup.dataArray[indexPath.row];
  155. return cell;
  156. }
  157. #pragma mark - UICollectionViewDelegate
  158. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. EMEmoticonCell *cell = (EMEmoticonCell *)[collectionView cellForItemAtIndexPath:indexPath];
  161. if (self.delegate && [self.delegate respondsToSelector:@selector(emoticonViewDidSelectedModel:)]) {
  162. [self.delegate emoticonViewDidSelectedModel:cell.model];
  163. }
  164. }
  165. #pragma mark - UICollectionViewDelegateFlowLayout
  166. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. return self.itemSize;
  169. }
  170. // 设置UIcollectionView整体的内边距(这样item不贴边显示)
  171. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  172. // 上 左 下 右
  173. return UIEdgeInsetsMake(self.itemMargin, self.itemMargin, self.itemMargin, self.itemMargin);
  174. }
  175. //设置minimumLineSpacing:cell上下之间最小的距离
  176. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  177. return self.itemMargin;
  178. }
  179. // 设置minimumInteritemSpacing:cell左右之间最小的距离
  180. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  181. return self.itemMargin;
  182. }
  183. @end