ChatEmojiView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // ChatEmojiView.m
  3. // BuguLive
  4. //
  5. // Created by 朱庆彬 on 2017/8/15.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "ChatEmojiView.h"
  9. #import "CommentEmoji.h"
  10. #import "EmojiButton.h"
  11. #import "EmojiObj.h"
  12. #import "EmojiScrollView.h"
  13. #import "IconButton.h"
  14. #import "SMPageControl.h"
  15. @interface ChatEmojiView () <UIScrollViewDelegate>
  16. {
  17. EmojiScrollView *scroll;
  18. IconButton *selectIcon;
  19. }
  20. @property (nonatomic, strong) SMPageControl *pageControl;
  21. @property (nonatomic, copy) NSArray *iconS;
  22. @end
  23. @implementation ChatEmojiView
  24. - (instancetype)init
  25. {
  26. return [self initWithFrame:CGRectZero];
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame
  29. {
  30. CGRect _frame_ = CGRectMake(0, 0, kScreenW, ChatEmojiView_Hight);
  31. _frame_.origin = frame.origin;
  32. if (self = [super initWithFrame:_frame_])
  33. {
  34. [self initUI];
  35. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  36. [self addEmjioWith:[CommentEmoji class]];
  37. });
  38. }
  39. self.y = kScreenH;
  40. return self;
  41. }
  42. - (void)setFrame:(CGRect)frame
  43. {
  44. if ((frame.size.height == ChatEmojiView_Hight) && (frame.size.width == kScreenW))
  45. {
  46. [super setFrame:frame];
  47. }
  48. }
  49. #pragma mark - 初始化界面
  50. - (void)initUI
  51. {
  52. CGRect frame = self.bounds;
  53. frame.size.height -= ChatEmojiView_Bottom_H;
  54. [self addScroll:frame]; /*scroll*/
  55. frame.origin.y = CGRectGetHeight(frame) - 2 * EmojiView_Border + 10;
  56. frame.size.height = EmojiView_Border;
  57. [self addPageControl:frame]; /*pagecontrol*/
  58. frame.origin.y = ChatEmojiView_Hight - ChatEmojiView_Bottom_H;
  59. frame.size.height = ChatEmojiView_Bottom_H;
  60. [self addBottom:frame]; /*bottom*/
  61. [self common:_iconS[0]];
  62. }
  63. - (void)addScroll:(CGRect)frame
  64. {
  65. scroll = [[EmojiScrollView alloc] initWithFrame:frame];
  66. scroll.delegate = self;
  67. [self addSubview:scroll];
  68. }
  69. - (void)addPageControl:(CGRect)frame
  70. {
  71. self.pageControl = [[SMPageControl alloc] initWithFrame:frame];
  72. [self.pageControl setCurrentPageIndicatorImage:[UIImage imageNamed:[NSString stringWithFormat:@"face.bundle/%@", @"compose_keyboard_dot_selected"]]];
  73. [self.pageControl setPageIndicatorImage:[UIImage imageNamed:[NSString stringWithFormat:@"face.bundle/%@", @"compose_keyboard_dot_normal"]]];
  74. self.pageControl.userInteractionEnabled = YES;
  75. [self addSubview:self.pageControl];
  76. }
  77. - (void)addBottom:(CGRect)frame
  78. {
  79. UIView *bottom = [[UIView alloc] initWithFrame:frame];
  80. //common
  81. IconButton *common = [[IconButton alloc] initWithFrame:CGRectMake(0, 0, ChatEmojiView_Bottom_W, ChatEmojiView_Bottom_H)];
  82. [common setImage:[UIImage imageNamed:[NSString stringWithFormat:@"face.bundle/%@", @"ic_emoji_blue"]] forState:UIControlStateNormal];
  83. [common addTarget:self action:@selector(common:) forControlEvents:UIControlEventTouchUpInside];
  84. [bottom addSubview:common];
  85. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bottom.bounds.size.width, 0.5)];
  86. line.backgroundColor = [UIColor lightGrayColor];
  87. [bottom addSubview:line];
  88. self.iconS = @[common];
  89. [self addSubview:bottom];
  90. //send
  91. UIButton *sendB = [[UIButton alloc] init];
  92. [sendB setTitle:ASLocalizedString(@"发送")forState:UIControlStateNormal];
  93. [sendB addTarget:self action:@selector(sendButtonAction) forControlEvents:UIControlEventTouchUpInside];
  94. frame = common.frame;
  95. frame.origin = CGPointMake(self.bounds.size.width - frame.size.width, 0);
  96. sendB.frame = frame;
  97. [sendB setBackgroundColor:kAppMainColor];
  98. [bottom addSubview:sendB];
  99. }
  100. - (void)addEmojisWithType:(ChatEmojiViewIconType)type
  101. {
  102. switch (type)
  103. {
  104. case ChatEmojiViewIconTypeCommon:
  105. [self common:_iconS[type]];
  106. break;
  107. case ChatEmojiViewIconTypeOther:
  108. // [self panda:_iconS[type]];
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. - (void)common:(IconButton *)sender
  115. {
  116. [self sender:sender];
  117. [self addEmjioWith:[CommentEmoji class]];
  118. }
  119. - (void)sender:(IconButton *)sender
  120. {
  121. if (sender)
  122. {
  123. if (selectIcon)
  124. selectIcon.backgroundColor = [UIColor clearColor];
  125. selectIcon = sender;
  126. selectIcon.backgroundColor = kAppGrayColor4;
  127. }
  128. }
  129. - (void)addEmjioWith:(Class) class
  130. {
  131. [self scrollInit];
  132. NSInteger count_lins = [class countInOneLine];
  133. NSInteger page_support = [class pageCountIsSupport];
  134. CGFloat space = (kScreenW - EmojiView_Border * 2.0f - count_lins * EmojiIMG_Width_Hight) / (count_lins - 1);
  135. scroll.contentSize = CGSizeMake(page_support * kScreenW, 0);
  136. self.pageControl.numberOfPages = page_support;
  137. for (int i = 0; i < page_support; i++)
  138. {
  139. NSArray *array = [class emojiObjsWithPage:i];
  140. for (int j = 0; j < array.count - 1; j++)
  141. {
  142. NSInteger lins_w = j / count_lins;
  143. NSInteger list_w = j % count_lins;
  144. CGRect frame = CGRectMake(EmojiView_Border + list_w * (space + EmojiIMG_Width_Hight) + (i * kScreenW), EmojiView_Border + lins_w * (EmojiIMG_Space_UP + EmojiIMG_Width_Hight), EmojiIMG_Width_Hight, EmojiIMG_Width_Hight);
  145. EmojiButton *button = [[EmojiButton alloc] initWithFrame:frame];
  146. button.emojiIcon = array[j];
  147. [button addTarget:self action:@selector(selectorThisIcon:) forControlEvents:UIControlEventTouchUpInside];
  148. [scroll addSubview:button];
  149. // UILongPressGestureRecognizer * loTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(loTaoAction:)];
  150. // [button addGestureRecognizer:loTap];
  151. }
  152. EmojiButton *del_B = [[EmojiButton alloc] initWithFrame:CGRectMake(EmojiView_Border + (count_lins - 1) * (space + EmojiIMG_Width_Hight) + (i * kScreenW), EmojiView_Border + (EmojiIMG_Lines - 1) * (EmojiIMG_Space_UP + EmojiIMG_Width_Hight), EmojiIMG_Width_Hight, EmojiIMG_Width_Hight)];
  153. del_B.emojiIcon = [array lastObject];
  154. [del_B addTarget:self action:@selector(deleteIcons) forControlEvents:UIControlEventTouchUpInside];
  155. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteAllIcons:)];
  156. longPress.minimumPressDuration = 0.8; //定义按的时间
  157. [del_B addGestureRecognizer:longPress];
  158. [scroll addSubview:del_B];
  159. }
  160. }
  161. #pragma mark - other logic action
  162. /*切换表情列表*/
  163. - (void) scrollInit
  164. {
  165. for (UIView *v in scroll.subviews)
  166. {
  167. if ([v isKindOfClass:[EmojiButton class]])
  168. {
  169. [v removeFromSuperview];
  170. }
  171. }
  172. [scroll setContentOffset:CGPointZero];
  173. }
  174. - (void)loTaoAction:(UILongPressGestureRecognizer *)loTap
  175. {
  176. }
  177. - (void)showEmotionName:(NSString *)name
  178. {
  179. #define W 80.0f
  180. #define H 40.0f
  181. CGFloat x = (self.superview.frame.size.width - 80.0f) / 2.0f;
  182. UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(x, self.superview.frame.origin.y, W, H)];
  183. lab.text = name;
  184. lab.alpha = 0.1f;
  185. lab.layer.masksToBounds = YES;
  186. lab.layer.cornerRadius = 10.0f;
  187. lab.textColor = [UIColor whiteColor];
  188. lab.backgroundColor = [UIColor grayColor];
  189. lab.textAlignment = NSTextAlignmentCenter;
  190. lab.font = [UIFont systemFontOfSize:15.0f];
  191. [self.superview.superview addSubview:lab];
  192. [UIView animateWithDuration:0.4f
  193. animations:^{
  194. lab.frame = CGRectMake(x, -H, W, H);
  195. lab.alpha = 1.0f;
  196. }
  197. completion:^(BOOL finished) {
  198. if (finished)
  199. {
  200. [UIView animateWithDuration:0.3f
  201. delay:0.5f
  202. options:UIViewAnimationOptionCurveEaseOut
  203. animations:^{
  204. lab.alpha = 0.0f;
  205. }
  206. completion:^(BOOL finished) {
  207. if (finished)
  208. [lab removeFromSuperview];
  209. }];
  210. }
  211. }];
  212. }
  213. #pragma mark - self delegate action
  214. - (void)selectorThisIcon:(EmojiButton *)sender
  215. {
  216. if ([self.delegate respondsToSelector:@selector(chatEmojiViewSelectEmojiIcon:)])
  217. {
  218. [self.delegate chatEmojiViewSelectEmojiIcon:sender.emojiIcon];
  219. }
  220. }
  221. - (void)deleteIcons
  222. {
  223. if ([self.delegate respondsToSelector:@selector(chatEmojiViewTouchUpinsideDeleteButton)])
  224. {
  225. [self.delegate chatEmojiViewTouchUpinsideDeleteButton];
  226. }
  227. }
  228. -(void)deleteAllIcons:(UILongPressGestureRecognizer *)gestureRecognizer{
  229. if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
  230. if ([self.delegate respondsToSelector:@selector(chatEmojiViewTouchDownDeleteButton)])
  231. {
  232. [self.delegate chatEmojiViewTouchDownDeleteButton];
  233. }
  234. }
  235. }
  236. - (void)sendButtonAction
  237. {
  238. if ([self.delegate respondsToSelector:@selector(chatEmojiViewTouchUpinsideSendButton)])
  239. {
  240. [self.delegate chatEmojiViewTouchUpinsideSendButton];
  241. }
  242. }
  243. #pragma mark - Scroll View Delegate
  244. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  245. {
  246. self.pageControl.currentPage = (scrollView.contentOffset.x / scrollView.bounds.size.width) + 0.5;
  247. }
  248. #pragma mark - Show And Hide
  249. - (void)show:(UIView *)superView{
  250. // [self requestModel];
  251. [superView addSubview:self.shadowView];
  252. [superView addSubview:self];
  253. self.hidden = NO;
  254. [UIView animateWithDuration:0.25 animations:^{
  255. self.shadowView.alpha = 1;
  256. self.y = kScreenH - self.height;
  257. }];
  258. }
  259. - (void)hide{
  260. if (self.delegate && [self.delegate respondsToSelector:@selector(clickHide)]) {
  261. [self.delegate clickHide];
  262. }
  263. [UIView animateWithDuration:0.25 animations:^{
  264. self.shadowView.alpha = 0;
  265. self.y = kScreenH;
  266. } completion:^(BOOL finished) {
  267. [self.shadowView removeFromSuperview];
  268. [self removeFromSuperview];
  269. }];
  270. }
  271. - (UIView *)shadowView{
  272. if (!_shadowView) {
  273. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  274. _shadowView.backgroundColor = kClearColor;
  275. _shadowView.userInteractionEnabled = YES;
  276. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  277. [_shadowView addGestureRecognizer:tap];
  278. }
  279. return _shadowView;
  280. }
  281. @end