FaceViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #import "FaceViewController.h"
  2. #import "SCGIFImageView.h"
  3. #import "JXMessageObject.h"
  4. #define BEGIN_FLAG @"["
  5. #define END_FLAG @"]"
  6. @implementation FaceViewController
  7. @synthesize delegate=_delegate,shortNameArrayC,shortNameArrayE;
  8. #define PAGE_COUNT 1
  9. - (id)initWithFrame:(CGRect)frame {
  10. self = [super initWithFrame:frame];
  11. // self.backgroundColor = [UIColor darkGrayColor];
  12. self.backgroundColor = [UIColor whiteColor];;
  13. /*
  14. faceArray = [[NSArray alloc]initWithObjects:@"[微笑]",@"[撇嘴]",@"[色]",@"[发呆]",@"[得意]",@"[流泪]",@"[害羞]",@"[闭嘴]",@"[睡]",@"[大哭]",
  15. @"[尴尬]",@"[发怒]",@"[调皮]",@"[龇牙]",@"[惊讶]",@"[难过]",@"[严肃]",@"[冷汗]",@"[抓狂]",@"[吐]",@"[偷笑]",@"[可爱]",@"[白眼]",@"[傲慢]",
  16. @"[饥饿]",@"[困]",@"[惊恐]",@"[流汗]",@"[憨笑]",@"[大兵]",@"[奋斗]",@"[咒骂]",@"[疑问]",@"[嘘]",@"[晕]",@"[折磨]",@"[衰]",@"[骷髅]",
  17. @"[敲打]",@"[再见]",@"[擦汗]",@"[抠鼻]",@"[鼓掌]",@"[糗大了]",@"[坏笑]",@"[左哼哼]",@"[右哼哼]",@"[哈欠]",@"[鄙视]",@"[委屈]",@"[快哭了]",
  18. @"[阴险]",@"[亲嘴]",@"[吓]",@"[可怜]",@"[菜刀]",@"[西瓜]",@"[啤酒]",@"[篮球]",@"[乒乓]",@"[咖啡]",@"[饭]",@"[猪头]",@"[玫瑰]",@"[凋谢]",
  19. @"[示爱]",@"[爱心]",@"[心碎]",@"[蛋糕]",@"[闪电]",@"[炸弹]",@"[刀]",@"[足球]",@"[瓢虫]",@"[便便]",@"[拥抱]",@"[月亮]",@"[太阳]",@"[礼物]",
  20. @"[强]",@"[弱]",@"[握手]",@"[胜利]",@"[抱拳]",@"[勾引]",@"[拳头]",@"[差劲]",@"[爱你]",@"[NO]",@"[OK]",@"[苹果]",@"[可爱狗]",@"[小熊]",@"[彩虹]",@"[皇冠]",@"[钻石]",nil];
  21. */
  22. //#warning 表情
  23. // faceArray = [[NSArray alloc]initWithObjects:Localized(@"[Smiley]"),Localized(@"[Proud]"),Localized(@"[Shy]"),Localized(@"[Sweat]"),Localized(@"[Grinning]"),Localized(@"[Shocked]"),Localized(@"[Happy]"),Localized(@"[Crying]"),Localized(@"[Vomiting]"),Localized(@"[Kiss]"),Localized(@"[Horny]"),Localized(@"[Sick]"),Localized(@"[Angry]"),Localized(@"[Cool]"),Localized(@"[Aggrieved]"),Localized(@"[Serious]"),Localized(@"[Doubt]"),Localized(@"[Dizzy]"),nil];
  24. // NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
  25. // faceArray = [FileInfo getFilesName:bundlePath];
  26. // NSLog(@"faceArray = %@", faceArray);
  27. //@"[微笑]",@"[得意]",@"[害羞]",@"[汗]",@"[奸笑]",@"[惊呆了]",@"[开心]",@"[哭]",@"[呕吐]",@"[亲亲]",@"[色眯眯]",@"[生病]",@"[生气]",@"[爽]",@"[委屈]",@"[严肃]",@"[疑问]",@"[晕]"
  28. //Localized(@"[Smile]"),Localized(@"[Proud]"),Localized(@"[Shy]"),Localized(@"[Sweat]"),Localized(@"[Smirking]"),Localized(@"[Shocked]"),Localized(@"[Happy]"),Localized(@"[Cry]"),Localized(@"[Sick]"),Localized(@"[Kiss]"),Localized(@"[Yasisi]"),Localized(@"[Ill]"),Localized(@"[Angry]"),Localized(@"[Cool]"),Localized(@"[Grievance]"),Localized(@"[Serious]"),Localized(@"[Doubt]"),Localized(@"[Halo]")
  29. // @"[Smile]",@"[Proud]",@"[Shy]",@"[Sweat]",@"[Smirking]",@"[Shocked]",@"[Happy]",@"[Cry]",@"[Sick]",@"[Kiss]",@"[Yasisi]",@"[Ill]",@"[Angry]",@"[Cool]",@"[Grievance]",@"[Serious]",@"[Doubt]",@"[Halo]"
  30. // imageArrayC = [[NSMutableArray alloc] init];
  31. // imageArrayE = [[NSMutableArray alloc] init];
  32. shortNameArrayC = [[NSMutableArray alloc] init];
  33. shortNameArrayE = [[NSMutableArray alloc] init];
  34. self.imageArray = [[NSMutableArray alloc] init];
  35. // for (int i = 0;i<[faceArray count];i++){
  36. //// NSString* s = [NSString stringWithFormat:@"f%.3d.png",i];
  37. // NSString* s = [faceArray objectAtIndex:i];
  38. // NSRange r;
  39. // r.length = [s length]-2;
  40. // r.location = 1;
  41. // s = [NSString stringWithFormat:@"%@.png",[s substringWithRange:r]];
  42. // [imageArray addObject:s];
  43. // }
  44. // NSString *firstStr = @"";
  45. // //英文
  46. // firstStr = @"e-";
  47. // for (int i = 0;i<[faceArray count];i++){
  48. // // NSString* s = [NSString stringWithFormat:@"f%.3d.png",i];
  49. // NSString* s = [faceArray objectAtIndex:i];
  50. // NSString *str = [s substringToIndex:2];
  51. // if ([str isEqualToString:firstStr]) {
  52. //
  53. // [imageArrayE addObject:s];
  54. // }
  55. // }
  56. //
  57. // for (int i = 0; i < imageArrayE.count; i ++) {
  58. // NSString *s = imageArrayE[i];
  59. // NSRange range1 = [s rangeOfString:@"_"];
  60. // NSRange range2 = [s rangeOfString:@"@"];
  61. // NSInteger length = range2.location - range1.location;
  62. // NSString *str = [s substringWithRange:NSMakeRange(range1.location + 1, length - 1)];
  63. // NSString *string = [NSString stringWithFormat:@"[%@]",str];
  64. // [shortNameArrayE addObject:string];
  65. // }
  66. //
  67. // firstStr = @"c-";
  68. // for (int i = 0;i<[faceArray count];i++){
  69. // // NSString* s = [NSString stringWithFormat:@"f%.3d.png",i];
  70. // NSString* s = [faceArray objectAtIndex:i];
  71. // NSString *str = [s substringToIndex:2];
  72. // if ([str isEqualToString:firstStr]) {
  73. //
  74. // [imageArrayC addObject:s];
  75. // }
  76. // }
  77. //
  78. // for (int i = 0; i < imageArrayC.count; i ++) {
  79. // NSString *s = imageArrayC[i];
  80. // NSRange range1 = [s rangeOfString:@"_"];
  81. // NSRange range2 = [s rangeOfString:@"@"];
  82. // NSInteger length = range2.location - range1.location;
  83. // NSString *str = [s substringWithRange:NSMakeRange(range1.location + 1, length - 1)];
  84. // NSString *string = [NSString stringWithFormat:@"[%@]",str];
  85. // [shortNameArrayC addObject:string];
  86. // }
  87. // 文件名
  88. for (NSInteger i = 0; i < g_constant.emojiArray.count; i ++) {
  89. NSDictionary *dic = g_constant.emojiArray[i];
  90. NSString *str = dic[@"filename"];
  91. [self.imageArray addObject:str];
  92. // 英文短名
  93. str = [NSString stringWithFormat:@"[%@]",dic[@"english"]];
  94. [shortNameArrayE addObject:str];
  95. // 中文短名
  96. str = [NSString stringWithFormat:@"[%@]",dic[@"chinese"]];
  97. [shortNameArrayC addObject:str];
  98. }
  99. // // 英文短名
  100. // for (NSInteger i = 0; i < g_constant.emojiArray.count; i ++) {
  101. // NSDictionary *dic = g_constant.emojiArray[i];
  102. // }
  103. // // 中文短名
  104. // for (NSInteger i = 0; i < g_constant.emojiArray.count; i ++) {
  105. // NSDictionary *dic = g_constant.emojiArray[i];
  106. // NSString *str = [NSString stringWithFormat:@"[%@]",dic[@"chinese"]];
  107. // [shortNameArrayC addObject:str];
  108. // }
  109. NSArray *languages = [NSLocale preferredLanguages];
  110. NSString *currentLanguage = [languages objectAtIndex:0];
  111. if ([currentLanguage rangeOfString:@"zh-"].location == NSNotFound) { //如果不是中文就返回
  112. // self.imageArray = imageArrayE;
  113. self.shortNameArray = shortNameArrayE;
  114. }else{
  115. // self.imageArray = imageArrayC;
  116. self.shortNameArray = shortNameArrayC;
  117. }
  118. [self create];
  119. return self;
  120. }
  121. - (void)dealloc {
  122. // [faceArray removeAllObjects];
  123. [_imageArray removeAllObjects];
  124. // [imageArrayC removeAllObjects];
  125. // [imageArrayE removeAllObjects];
  126. // [faceArray release];
  127. // [self.imageArray release];
  128. // [super dealloc];
  129. }
  130. -(void)create{
  131. int iconWith = 32;
  132. int margin = 17;
  133. int tempN = JX_SCREEN_WIDTH / (iconWith+margin);
  134. // int tempN = 8;
  135. NSInteger pageCount = self.imageArray.count / (tempN * 3 - 1);
  136. if (self.imageArray.count % (tempN * 3 - 1) != 0) {
  137. pageCount = pageCount + 1;
  138. }
  139. _sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-20)];
  140. _sv.contentSize = CGSizeMake(WIDTH_PAGE*pageCount, self.frame.size.height-20);
  141. _sv.pagingEnabled = YES;
  142. _sv.scrollEnabled = YES;
  143. _sv.delegate = self;
  144. _sv.showsVerticalScrollIndicator = NO;
  145. _sv.showsHorizontalScrollIndicator = NO;
  146. _sv.userInteractionEnabled = YES;
  147. _sv.minimumZoomScale = 1;
  148. _sv.maximumZoomScale = 1;
  149. _sv.decelerationRate = 0.01f;
  150. _sv.backgroundColor = [UIColor clearColor];
  151. [self addSubview:_sv];
  152. // [_sv release];
  153. // int tempN = (JX_SCREEN_WIDTH <= 320) ? 8:((JX_SCREEN_WIDTH >= 414) ? 10:9);
  154. int startX = (JX_SCREEN_WIDTH - tempN * iconWith - (tempN + 1) * margin) / 2;
  155. int n = 0;
  156. // UIImage *tempImage;
  157. NSString* s;
  158. for(int i=0;i<pageCount;i++){
  159. int x=WIDTH_PAGE*i + startX,y=0;
  160. for(int j=0;j<tempN * 3 - 1;j++){
  161. if(n>=[self.imageArray count])
  162. break;
  163. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  164. button.frame = CGRectMake(x+margin, y+10, iconWith, iconWith);
  165. button.tag = n;
  166. s = [self.imageArray objectAtIndex:n];
  167. [button addTarget:self action:@selector(actionSelect:)forControlEvents:UIControlEventTouchUpInside];
  168. if( (j+1) % tempN == 0){
  169. x = WIDTH_PAGE*i + startX;
  170. y += 50;
  171. }else
  172. x += (iconWith+margin);
  173. n++;
  174. UIImage * emojiImage = [UIImage imageNamed:s];
  175. if (!emojiImage)
  176. NSLog(@"kong:%@",s);
  177. [button setBackgroundImage:emojiImage forState:UIControlStateNormal];
  178. [_sv addSubview:button];
  179. }
  180. s = @"im_delete_button_press";
  181. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  182. [button addTarget:self action:@selector(actionDelete:)forControlEvents:UIControlEventTouchUpInside];
  183. button.frame = CGRectMake(_sv.frame.size.width * (i + 1) - 33-15, 115, 33, 23);
  184. [button setBackgroundImage:[UIImage imageNamed:s] forState:UIControlStateNormal];
  185. [_sv addSubview:button];
  186. }
  187. _pc = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 115 + 23 + 12, JX_SCREEN_WIDTH-200, 30)];
  188. _pc.numberOfPages = pageCount;
  189. _pc.pageIndicatorTintColor = [UIColor grayColor];
  190. _pc.currentPageIndicatorTintColor = [UIColor blackColor];
  191. [_pc addTarget:self action:@selector(actionPage) forControlEvents:UIControlEventTouchUpInside];
  192. [self addSubview:_pc];
  193. // 发送
  194. UIButton *sendButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
  195. [sendButton setFont:[UIFont systemFontOfSize:14]];
  196. sendButton.frame = CGRectMake(self.frame.size.width - 40 - 15, 115 + 23 + 12, 40, 23);
  197. [sendButton setTitle:@"发送" forState:(UIControlStateNormal)];
  198. [sendButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
  199. [sendButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateHighlighted)];
  200. sendButton.layer.cornerRadius = 5;
  201. sendButton.layer.masksToBounds = YES;
  202. sendButton.layer.borderWidth = 1;
  203. sendButton.layer.borderColor = [UIColor lightGrayColor].CGColor;;
  204. [sendButton addTarget:self action:@selector(sendEmoji) forControlEvents:(UIControlEventTouchUpInside)];
  205. [self addSubview:sendButton];
  206. }
  207. - (void)sendEmoji {
  208. [g_notify postNotificationName:kSendInputNotifaction object:nil userInfo:nil];
  209. }
  210. -(void)actionSelect:(UIView*)sender
  211. {
  212. NSString *imageName = self.imageArray[sender.tag];
  213. NSString* shortName = [self.shortNameArrayE objectAtIndex:sender.tag];
  214. if ([self.delegate respondsToSelector:@selector(selectImageNameString:ShortName:isSelectImage:)]) {
  215. [self.delegate selectImageNameString:imageName ShortName:shortName isSelectImage:YES];
  216. }
  217. // NSString* s = [self.shortNameArray objectAtIndex:sender.tag];
  218. // if( [_delegate isKindOfClass:[UITextField class]] ){
  219. // UITextField* p = _delegate;
  220. // p.tag = kWCMessageTypeText;
  221. // NSString* t = @"";
  222. // if([p.text length]<=0)
  223. // p.text = t;
  224. // p.text = [p.text stringByAppendingString:s];
  225. // [p setNeedsDisplay];
  226. // p = nil;
  227. // }
  228. }
  229. -(IBAction)actionDelete:(UIView*)sender{
  230. if ([self.delegate respondsToSelector:@selector(faceViewDeleteAction)]) {
  231. [self.delegate faceViewDeleteAction];
  232. }
  233. // if( [_delegate isKindOfClass:[UITextField class]] ){
  234. // UITextField* p = _delegate;
  235. // NSString* s = p.text;
  236. //
  237. // if([s length]<=0)
  238. // return;
  239. // int n=-1;
  240. // if( [s characterAtIndex:[s length]-1] == ']'){
  241. // for(int i=[s length]-1;i>=0;i--){
  242. // if( [s characterAtIndex:i] == '[' ){
  243. // n = i;
  244. // break;
  245. // }
  246. // }
  247. // }
  248. // if(n>=0)
  249. // p.text = [s substringWithRange:NSMakeRange(0,n)];
  250. // else
  251. // p.text = [s substringToIndex:[s length]-1];
  252. // p = nil;
  253. // }
  254. }
  255. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  256. {
  257. int index = scrollView.contentOffset.x/320;
  258. int mod = fmod(scrollView.contentOffset.x,320);
  259. if( mod >= 160)
  260. index++;
  261. _pc.currentPage = index;
  262. // [self setPage];
  263. }
  264. - (void) setPage
  265. {
  266. _sv.contentOffset = CGPointMake(WIDTH_PAGE*_pc.currentPage, 0.0f);
  267. // NSLog(@"setPage:%d,%ld",_sv.contentOffset,_pc.currentPage);
  268. [_pc setNeedsDisplay];
  269. }
  270. -(void)actionPage{
  271. [self setPage];
  272. }
  273. /*
  274. -(void)createRecognizer{
  275. UIPanGestureRecognizer *panGR =
  276. [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(objectDidDragged:)];
  277. //限定操作的触点数
  278. [panGR setMaximumNumberOfTouches:1];
  279. [panGR setMinimumNumberOfTouches:1];
  280. //将手势添加到draggableObj里
  281. [self addGestureRecognizer:panGR];
  282. }
  283. - (void)objectDidDragged:(UIPanGestureRecognizer *)sender {
  284. if (sender.state == UIGestureRecognizerStateEnded){
  285. CGPoint offset = [sender translationInView:g_App.window];
  286. if(offset.y>20 || offset.y<-20)
  287. return;
  288. if(offset.x>0)
  289. _pc.currentPage++;
  290. else
  291. _pc.currentPage--;
  292. [self setPage];
  293. }
  294. }*/
  295. @end