TCFaceView.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // TCFaceView.m
  3. // QimoQM
  4. //
  5. // Created by TuChuan on 15/5/13.
  6. // Copyright (c) 2015年 七陌科技. All rights reserved.
  7. //
  8. #import "TCFaceView.h"
  9. #define NumPerLine 7
  10. #define Lines 3
  11. #define FaceSize 34
  12. /*
  13. ** 两边边缘间隔
  14. */
  15. #define EdgeDistance 20
  16. /*
  17. ** 上下边缘间隔
  18. */
  19. #define EdgeInterVal 5
  20. @implementation TCFaceView
  21. - (id)initWithFrame:(CGRect)frame forIndexPath:(int)index
  22. {
  23. self = [super initWithFrame:frame];
  24. if (self) {
  25. // 水平间隔
  26. CGFloat horizontalInterval = (CGRectGetWidth(self.bounds)-NumPerLine*FaceSize -2*EdgeDistance)/(NumPerLine-1);
  27. // 上下垂直间隔
  28. CGFloat verticalInterval = (CGRectGetHeight(self.bounds)-2*EdgeInterVal -Lines*FaceSize)/(Lines-1);
  29. NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"QMEmoticon" ofType:@"bundle"];
  30. for (int i = 0; i<Lines; i++)
  31. {
  32. for (int x = 0;x<NumPerLine;x++)
  33. {
  34. UIButton *expressionButton =[UIButton buttonWithType:UIButtonTypeCustom];
  35. [self addSubview:expressionButton];
  36. [expressionButton setFrame:CGRectMake(x*FaceSize+EdgeDistance+x*horizontalInterval,
  37. i*FaceSize +i*verticalInterval+EdgeInterVal,
  38. FaceSize,
  39. FaceSize)];
  40. if (i*7+x+1 ==21) {
  41. [expressionButton setBackgroundImage:[UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/emoji_delete", bundlePath]]
  42. forState:UIControlStateNormal];
  43. expressionButton.tag = 101;
  44. }else{
  45. NSString *imageStr = [NSString stringWithFormat:@"emoji_%d",(index*20+i*7+x+1)];
  46. NSString *imagePath = [NSString stringWithFormat:@"%@/%@", bundlePath, imageStr];
  47. [expressionButton setBackgroundImage:[UIImage imageWithContentsOfFile:imagePath] forState:UIControlStateNormal];
  48. expressionButton.tag = 20*index+i*7+x+1;
  49. }
  50. [expressionButton addTarget:self
  51. action:@selector(faceClick:)
  52. forControlEvents:UIControlEventTouchUpInside];
  53. }
  54. }
  55. }
  56. return self;
  57. }
  58. - (void)faceClick:(UIButton *)button{
  59. NSString *faceName;
  60. BOOL isDelete;
  61. if (button.tag ==101){
  62. faceName = nil;
  63. isDelete = YES;
  64. }else{
  65. NSString *expressstring = [NSString stringWithFormat:@"emoji_%ld.png",(long)button.tag];
  66. NSString *plistStr = [[NSBundle mainBundle]pathForResource:@"expressionImage" ofType:@"plist"];
  67. NSDictionary *plistDic = [[NSDictionary alloc]initWithContentsOfFile:plistStr];
  68. for (int j = 0; j<[[plistDic allKeys]count]; j++)
  69. {
  70. if ([[plistDic objectForKey:[[plistDic allKeys]objectAtIndex:j]]
  71. isEqualToString:[NSString stringWithFormat:@"%@",expressstring]])
  72. {
  73. faceName = [[plistDic allKeys]objectAtIndex:j];
  74. }
  75. }
  76. isDelete = NO;
  77. }
  78. if (self.delegate && [self.delegate respondsToSelector:@selector(didSelecteFace:andIsSelecteDelete:)]) {
  79. [self.delegate didSelecteFace:faceName andIsSelecteDelete:isDelete];
  80. }
  81. }
  82. @end