EaseFaceView.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "EaseFaceView.h"
  13. #import "EaseEmotionManager.h"
  14. #import "EaseLocalDefine.h"
  15. #define kButtomNum 5
  16. @interface EaseFaceView ()
  17. {
  18. UIScrollView *_bottomScrollView;
  19. NSInteger _currentSelectIndex;
  20. NSArray *_emotionManagers;
  21. }
  22. @property (nonatomic, strong) EaseFacialView *facialView;
  23. @end
  24. @implementation EaseFaceView
  25. - (id)initWithFrame:(CGRect)frame
  26. {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. [self addSubview:self.facialView];
  30. [self _setupButtom];
  31. }
  32. return self;
  33. }
  34. - (void)willMoveToSuperview:(UIView *)newSuperview
  35. {
  36. if (newSuperview) {
  37. [self reloadEmotionData];
  38. }
  39. }
  40. #pragma mark - private
  41. - (EaseFacialView*)facialView
  42. {
  43. if (_facialView == nil) {
  44. _facialView = [[EaseFacialView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 150)];
  45. _facialView.delegate = self;
  46. }
  47. return _facialView;
  48. }
  49. - (void)_setupButtom
  50. {
  51. _currentSelectIndex = 1000;
  52. _bottomScrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(0, CGRectGetMaxY(_facialView.frame), 4 * CGRectGetWidth(_facialView.frame)/5, self.frame.size.height - CGRectGetHeight(_facialView.frame))];
  53. _bottomScrollView.showsHorizontalScrollIndicator = NO;
  54. [self addSubview:_bottomScrollView];
  55. [self _setupButtonScrollView];
  56. UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
  57. sendButton.frame = CGRectMake((kButtomNum-1)*CGRectGetWidth(_facialView.frame)/kButtomNum, CGRectGetMaxY(_facialView.frame), CGRectGetWidth(_facialView.frame)/kButtomNum, CGRectGetHeight(_bottomScrollView.frame));
  58. [sendButton setBackgroundColor:[UIColor colorWithRed:30 / 255.0 green:167 / 255.0 blue:252 / 255.0 alpha:1.0]];
  59. [sendButton setTitle:NSEaseLocalizedString(@"send", @"Send") forState:UIControlStateNormal];
  60. [sendButton addTarget:self action:@selector(sendFace) forControlEvents:UIControlEventTouchUpInside];
  61. [self addSubview:sendButton];
  62. }
  63. - (void)_setupButtonScrollView
  64. {
  65. NSInteger number = [_emotionManagers count];
  66. if (number <= 1) {
  67. return;
  68. }
  69. for (UIView *view in [_bottomScrollView subviews]) {
  70. [view removeFromSuperview];
  71. }
  72. for (int i = 0; i < number; i++) {
  73. UIButton *defaultButton = [UIButton buttonWithType:UIButtonTypeCustom];
  74. defaultButton.frame = CGRectMake(i * CGRectGetWidth(_bottomScrollView.frame)/(kButtomNum-1), 0, CGRectGetWidth(_bottomScrollView.frame)/(kButtomNum-1), CGRectGetHeight(_bottomScrollView.frame));
  75. EaseEmotionManager *emotionManager = [_emotionManagers objectAtIndex:i];
  76. if (emotionManager.emotionType == EMEmotionDefault) {
  77. EaseEmotion *emotion = [emotionManager.emotions objectAtIndex:0];
  78. [defaultButton setTitle:emotion.emotionThumbnail forState:UIControlStateNormal];
  79. } else {
  80. [defaultButton setImage:emotionManager.tagImage forState:UIControlStateNormal];
  81. [defaultButton setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
  82. defaultButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
  83. }
  84. [defaultButton setBackgroundColor:[UIColor clearColor]];
  85. defaultButton.layer.borderWidth = 0.5;
  86. defaultButton.layer.borderColor = [UIColor whiteColor].CGColor;
  87. [defaultButton addTarget:self action:@selector(didSelect:) forControlEvents:UIControlEventTouchUpInside];
  88. defaultButton.tag = 1000 + i;
  89. [_bottomScrollView addSubview:defaultButton];
  90. }
  91. [_bottomScrollView setContentSize:CGSizeMake(number*CGRectGetWidth(_bottomScrollView.frame)/(kButtomNum-1), CGRectGetHeight(_bottomScrollView.frame))];
  92. [self reloadEmotionData];
  93. }
  94. - (void)_clearupButtomScrollView
  95. {
  96. for (UIView *view in [_bottomScrollView subviews]) {
  97. [view removeFromSuperview];
  98. }
  99. }
  100. #pragma mark - action
  101. - (void)didSelect:(id)sender
  102. {
  103. UIButton *btn = (UIButton*)sender;
  104. UIButton *lastBtn = (UIButton*)[_bottomScrollView viewWithTag:_currentSelectIndex];
  105. lastBtn.selected = NO;
  106. _currentSelectIndex = btn.tag;
  107. btn.selected = YES;
  108. NSInteger index = btn.tag - 1000;
  109. [_facialView loadFacialViewWithPage:index];
  110. }
  111. - (void)reloadEmotionData
  112. {
  113. NSInteger index = _currentSelectIndex - 1000;
  114. if (index < [_emotionManagers count]) {
  115. [_facialView loadFacialView:_emotionManagers size:CGSizeMake(30, 30)];
  116. }
  117. }
  118. #pragma mark - FacialViewDelegate
  119. -(void)selectedFacialView:(NSString*)str{
  120. if (_delegate) {
  121. [_delegate selectedFacialView:str isDelete:NO];
  122. }
  123. }
  124. -(void)deleteSelected:(NSString *)str{
  125. if (_delegate) {
  126. [_delegate selectedFacialView:str isDelete:YES];
  127. }
  128. }
  129. - (void)sendFace
  130. {
  131. if (_delegate) {
  132. [_delegate sendFace];
  133. }
  134. }
  135. - (void)sendFace:(EaseEmotion *)emotion
  136. {
  137. if (_delegate) {
  138. [_delegate sendFaceWithEmotion:emotion];
  139. }
  140. }
  141. #pragma mark - public
  142. - (BOOL)stringIsFace:(NSString *)string
  143. {
  144. if ([_facialView.faces containsObject:string]) {
  145. return YES;
  146. }
  147. return NO;
  148. }
  149. - (void)setEmotionManagers:(NSArray *)emotionManagers
  150. {
  151. _emotionManagers = emotionManagers;
  152. for (EaseEmotionManager *emotionManager in _emotionManagers) {
  153. if (emotionManager.emotionType != EMEmotionGif) {
  154. NSMutableArray *array = [NSMutableArray arrayWithArray:emotionManager.emotions];
  155. NSInteger maxRow = emotionManager.emotionRow;
  156. NSInteger maxCol = emotionManager.emotionCol;
  157. NSInteger count = 1;
  158. while (1) {
  159. NSInteger index = maxRow * maxCol * count - 1;
  160. if (index >= [array count]) {
  161. [array addObject:@""];
  162. break;
  163. } else {
  164. [array insertObject:@"" atIndex:index];
  165. }
  166. count++;
  167. }
  168. emotionManager.emotions = array;
  169. }
  170. }
  171. [self _setupButtonScrollView];
  172. }
  173. @end