EaseFacialView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseFacialView.h"
  13. #import "EaseEmoji.h"
  14. #import "EaseFaceView.h"
  15. #import "EaseEmotionManager.h"
  16. @interface UIButton (UIButtonImageWithLable)
  17. - (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType;
  18. @end
  19. @implementation UIButton (UIButtonImageWithLable)
  20. - (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
  21. //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
  22. CGSize titleSize;
  23. if ([NSString instancesRespondToSelector:@selector(sizeWithAttributes:)]) {
  24. titleSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}];
  25. } else {
  26. titleSize = [title sizeWithFont:[UIFont systemFontOfSize:10]];
  27. }
  28. [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
  29. [self setImageEdgeInsets:UIEdgeInsetsMake(0,
  30. 0.0,
  31. 20,
  32. 0)];
  33. [self setImage:image forState:stateType];
  34. [self.titleLabel setContentMode:UIViewContentModeCenter];
  35. [self.titleLabel setBackgroundColor:[UIColor clearColor]];
  36. [self.titleLabel setFont:[UIFont systemFontOfSize:10]];
  37. [self setTitleColor:[UIColor blackColor] forState:stateType];
  38. [self setTitleEdgeInsets:UIEdgeInsetsMake(CGRectGetHeight(self.bounds)-20,
  39. -image.size.width,
  40. 0,
  41. 0.0)];
  42. [self setTitle:title forState:stateType];
  43. }
  44. @end
  45. @protocol EaseCollectionViewCellDelegate
  46. @optional
  47. - (void)didSendEmotion:(EaseEmotion*)emotion;
  48. @end
  49. @interface EaseCollectionViewCell : UICollectionViewCell
  50. @property (nonatomic, weak) id<EaseCollectionViewCellDelegate> delegate;
  51. @property (nonatomic, strong) UIButton *imageButton;
  52. @property (nonatomic, strong) EaseEmotion *emotion;
  53. @end
  54. @implementation EaseCollectionViewCell
  55. - (id)initWithFrame:(CGRect)frame
  56. {
  57. self = [super initWithFrame:frame];
  58. if (self) {
  59. _imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
  60. _imageButton.frame = self.bounds;
  61. _imageButton.userInteractionEnabled = YES;
  62. [self.contentView addSubview:_imageButton];
  63. }
  64. return self;
  65. }
  66. - (void)setFrame:(CGRect)frame
  67. {
  68. [super setFrame:frame];
  69. _imageButton.frame = self.bounds;
  70. }
  71. - (void)setEmotion:(EaseEmotion *)emotion
  72. {
  73. _emotion = emotion;
  74. if ([emotion isKindOfClass:[EaseEmotion class]]) {
  75. if (emotion.emotionType == EMEmotionGif) {
  76. [_imageButton setImage:[UIImage imageNamed:emotion.emotionThumbnail] withTitle:emotion.emotionTitle forState:UIControlStateNormal];
  77. } else if (emotion.emotionType == EMEmotionPng) {
  78. [_imageButton setImage:[UIImage imageNamed:emotion.emotionThumbnail] forState:UIControlStateNormal];
  79. _imageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  80. [_imageButton setTitle:nil forState:UIControlStateNormal];
  81. [_imageButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  82. [_imageButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  83. } else {
  84. [_imageButton.titleLabel setFont:[UIFont fontWithName:@"AppleColorEmoji" size:29.0]];
  85. [_imageButton setTitle:emotion.emotionThumbnail forState:UIControlStateNormal];
  86. [_imageButton setImage:nil forState:UIControlStateNormal];
  87. [_imageButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  88. [_imageButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  89. }
  90. [_imageButton addTarget:self action:@selector(sendEmotion:) forControlEvents:UIControlEventTouchUpInside];
  91. } else {
  92. [_imageButton setTitle:nil forState:UIControlStateNormal];
  93. [_imageButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  94. [_imageButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  95. [_imageButton setImage:[UIImage imageNamed:@"EaseUIResource.bundle/faceDelete"] forState:UIControlStateNormal];
  96. [_imageButton setImage:[UIImage imageNamed:@"EaseUIResource.bundle/faceDelete_select"] forState:UIControlStateHighlighted];
  97. [_imageButton addTarget:self action:@selector(sendEmotion:) forControlEvents:UIControlEventTouchUpInside];
  98. }
  99. }
  100. - (void)sendEmotion:(id)sender
  101. {
  102. if (_delegate) {
  103. if ([_emotion isKindOfClass:[EaseEmotion class]]) {
  104. [_delegate didSendEmotion:_emotion];
  105. } else {
  106. [_delegate didSendEmotion:nil];
  107. }
  108. }
  109. }
  110. @end
  111. @interface EaseFacialView () <UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,EaseCollectionViewCellDelegate>
  112. {
  113. CGFloat _itemWidth;
  114. CGFloat _itemHeight;
  115. }
  116. @property (nonatomic, strong) UIScrollView *scrollview;
  117. @property (nonatomic, strong) UIPageControl *pageControl;
  118. @property (nonatomic, strong) UICollectionView *collectionView;
  119. @property (nonatomic, strong) NSArray *emotionManagers;
  120. @end
  121. @implementation EaseFacialView
  122. - (id)initWithFrame:(CGRect)frame
  123. {
  124. self = [super initWithFrame:frame];
  125. if (self) {
  126. _pageControl = [[UIPageControl alloc] init];
  127. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  128. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  129. _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
  130. [self.collectionView registerClass:[EaseCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
  131. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
  132. _collectionView.backgroundColor = [UIColor clearColor];
  133. _collectionView.delegate = self;
  134. _collectionView.dataSource = self;
  135. _collectionView.showsVerticalScrollIndicator = NO;
  136. _collectionView.showsHorizontalScrollIndicator = NO;
  137. _collectionView.alwaysBounceHorizontal = YES;
  138. _collectionView.pagingEnabled = YES;
  139. _collectionView.userInteractionEnabled = YES;
  140. // [self addSubview:_scrollview];
  141. [self addSubview:_pageControl];
  142. [self addSubview:_collectionView];
  143. }
  144. return self;
  145. }
  146. #pragma mark - UICollectionViewDataSource
  147. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  148. {
  149. if (section < [_emotionManagers count]) {
  150. EaseEmotionManager *emotionManager = [_emotionManagers objectAtIndex:section];
  151. return [emotionManager.emotions count];
  152. }
  153. return 0;
  154. }
  155. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  156. {
  157. if (_emotionManagers == nil || [_emotionManagers count] == 0) {
  158. return 1;
  159. }
  160. return [_emotionManagers count];
  161. }
  162. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. static NSString* identify = @"collectionCell";
  165. EaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
  166. if (!cell) {
  167. }
  168. [cell sizeToFit];
  169. EaseEmotionManager *emotionManager = [_emotionManagers objectAtIndex:indexPath.section];
  170. EaseEmotion *emotion = [emotionManager.emotions objectAtIndex:indexPath.row];
  171. cell.emotion = emotion;
  172. cell.userInteractionEnabled = YES;
  173. cell.delegate = self;
  174. return cell;
  175. }
  176. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  177. {
  178. EaseEmotionManager *emotionManager = [_emotionManagers objectAtIndex:section];
  179. CGFloat itemWidth = self.frame.size.width / emotionManager.emotionCol;
  180. NSInteger pageSize = emotionManager.emotionRow*emotionManager.emotionCol;
  181. NSInteger lastPage = (pageSize - [emotionManager.emotions count]%pageSize);
  182. if (lastPage < emotionManager.emotionRow ||[emotionManager.emotions count]%pageSize == 0) {
  183. return CGSizeMake(0, 0);
  184. } else{
  185. NSInteger size = lastPage/emotionManager.emotionRow;
  186. return CGSizeMake(size*itemWidth, self.frame.size.height);
  187. }
  188. }
  189. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  190. {
  191. UICollectionReusableView *reusableview = nil;
  192. if (kind == UICollectionElementKindSectionHeader){
  193. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
  194. reusableview = headerView;
  195. }
  196. if (kind == UICollectionElementKindSectionFooter){
  197. UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
  198. reusableview = footerview;
  199. }
  200. return reusableview;
  201. }
  202. #pragma mark --UICollectionViewDelegateFlowLayout
  203. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. EaseEmotionManager *emotionManager = [_emotionManagers objectAtIndex:indexPath.section];
  206. NSInteger maxRow = emotionManager.emotionRow;
  207. NSInteger maxCol = emotionManager.emotionCol;
  208. CGFloat itemWidth = self.frame.size.width / maxCol;
  209. CGFloat itemHeight = (self.frame.size.height) / maxRow;
  210. return CGSizeMake(itemWidth, itemHeight);
  211. }
  212. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  213. {
  214. return UIEdgeInsetsMake(0, 0, 0, 0);
  215. }
  216. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  217. {
  218. return 0.0f;
  219. }
  220. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  221. {
  222. return 0.0f;
  223. }
  224. #pragma mark - UICollectionViewDelegate
  225. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  226. {
  227. }
  228. -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
  229. {
  230. return YES;
  231. }
  232. #pragma makr - EaseCollectionViewCellDelegate
  233. - (void)didSendEmotion:(EaseEmotion *)emotion
  234. {
  235. if (emotion) {
  236. if (emotion.emotionType == EMEmotionDefault) {
  237. if (_delegate) {
  238. [_delegate selectedFacialView:emotion.emotionId];
  239. }
  240. } else {
  241. if (_delegate) {
  242. [_delegate sendFace:emotion];
  243. }
  244. }
  245. } else {
  246. [_delegate deleteSelected:nil];
  247. }
  248. }
  249. -(void)loadFacialView:(NSArray*)emotionManagers size:(CGSize)size
  250. {
  251. for (UIView *view in [self.scrollview subviews]) {
  252. [view removeFromSuperview];
  253. }
  254. _emotionManagers = emotionManagers;
  255. [_collectionView reloadData];
  256. }
  257. -(void)loadFacialViewWithPage:(NSInteger)page
  258. {
  259. [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:page]
  260. atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
  261. animated:NO];
  262. CGPoint offSet = _collectionView.contentOffset;
  263. if (page == 0) {
  264. [_collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
  265. } else {
  266. [_collectionView setContentOffset:CGPointMake(CGRectGetWidth(self.frame)*((int)(offSet.x/CGRectGetWidth(self.frame))+1), 0) animated:NO];
  267. }
  268. // [_collectionView setContentOffset:CGPointMake(CGRectGetWidth(self.frame)*2, 0) animated:NO];
  269. }
  270. #pragma mark - UIScrollViewDelegate
  271. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  272. {
  273. }
  274. @end