PasterAddView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // PasterSelectView.m
  3. // TXLiteAVDemo
  4. //
  5. // Created by xiang zhang on 2017/10/31.
  6. // Copyright © 2017年 Tencent. All rights reserved.
  7. //
  8. #import "UIView+Additions.h"
  9. #import "PasterAddView.h"
  10. @implementation PasterQipaoInfo
  11. @end
  12. @implementation PasterAnimateInfo
  13. @end
  14. @implementation PasterStaticInfo
  15. @end
  16. @implementation PasterAddView
  17. {
  18. UIScrollView * _selectView;
  19. NSArray * _pasterList;
  20. NSString * _boundPath;
  21. UIButton * _animateBtn;
  22. UIButton * _staticBtn;
  23. UIButton * _qipaoBtn;
  24. UIButton * _closeBtn;
  25. PasterType _pasterType;
  26. }
  27. - (instancetype) initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. CGFloat btnWidth = 100 * kScaleX;
  32. CGFloat btnHeight = 46 * kScaleY;
  33. _animateBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width / 2 - btnWidth, 0 , btnWidth, btnHeight)];
  34. [_animateBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  35. [_animateBtn setTitle:NSLocalizedString(@"PasterAddView.PasterDynamic", nil) forState:UIControlStateNormal];
  36. [_animateBtn sizeToFit];
  37. _animateBtn.left = self.width / 2 - _animateBtn.width - 2;
  38. [_animateBtn addTarget:self action:@selector(onAnimateBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  39. [self addSubview:_animateBtn];
  40. _staticBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width / 2, 0 , btnWidth, btnHeight)];
  41. [_staticBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  42. [_staticBtn setTitle:NSLocalizedString(@"PasterAddView.PasterStatic", nil) forState:UIControlStateNormal];
  43. [_staticBtn sizeToFit];
  44. _staticBtn.left = self.width / 2 + 2;
  45. [_staticBtn addTarget:self action:@selector(onStaticBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  46. [self addSubview:_staticBtn];
  47. _closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 45, 8 , 30, 30)];
  48. [_closeBtn setImage:[UIImage imageNamed:@"closePaster_normal"] forState:UIControlStateNormal];
  49. [_closeBtn setImage:[UIImage imageNamed:@"closePaster_press"] forState:UIControlStateHighlighted];
  50. [_closeBtn addTarget:self action:@selector(onClose) forControlEvents:UIControlEventTouchUpInside];
  51. [self addSubview:_closeBtn];
  52. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 46 * kScaleY, self.width, 1)];
  53. lineView.backgroundColor = RGB(53, 59, 72);
  54. [self addSubview:lineView];
  55. _qipaoBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width / 2 - btnWidth / 2, 0 , btnWidth, btnHeight)];
  56. [_qipaoBtn setTitleColor:UIColorFromRGB(0x0accac) forState:UIControlStateNormal];
  57. [_qipaoBtn setTitle:NSLocalizedString(@"PasterAddView.ChooseBubbleSub", nil) forState:UIControlStateNormal];
  58. [_qipaoBtn sizeToFit];
  59. _qipaoBtn.frame = CGRectMake(self.width / 2 - _qipaoBtn.width / 2, 0 , _qipaoBtn.width, btnHeight);
  60. _qipaoBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
  61. [_qipaoBtn addTarget:self action:@selector(onQipaoBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  62. [self addSubview:_qipaoBtn];
  63. _selectView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, lineView.bottom + 10 * kScaleY, self.width, self.height - lineView.bottom)];
  64. [self addSubview:_selectView];
  65. self.backgroundColor = UIColorFromRGB(0x1F2531);
  66. }
  67. return self;
  68. }
  69. - (void)setPasterType:(PasterType)pasterType
  70. {
  71. _pasterType = pasterType;
  72. if (_pasterType == PasterType_Animate || _pasterType == PasterType_static) {
  73. _animateBtn.hidden = NO;
  74. _staticBtn.hidden = NO;
  75. _qipaoBtn.hidden = YES;
  76. }else{
  77. _animateBtn.hidden = YES;
  78. _staticBtn.hidden = YES;
  79. _qipaoBtn.hidden = NO;
  80. }
  81. [self reloadSelectView];
  82. }
  83. - (void)onAnimateBtnClicked:(UIButton *)btn
  84. {
  85. [_animateBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  86. [_staticBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  87. _pasterType = PasterType_Animate;
  88. [self reloadSelectView];
  89. }
  90. - (void)onStaticBtnClicked:(UIButton *)btn
  91. {
  92. [_animateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  93. [_staticBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  94. _pasterType = PasterType_static;
  95. [self reloadSelectView];
  96. }
  97. - (void)onQipaoBtnClicked:(UIButton *)btn
  98. {
  99. _pasterType = PasterType_Qipao;
  100. [self reloadSelectView];
  101. }
  102. -(void)onClose
  103. {
  104. self.hidden = YES;
  105. }
  106. - (void)reloadSelectView;
  107. {
  108. switch (_pasterType) {
  109. case PasterType_Animate:
  110. {
  111. _boundPath = [[NSBundle mainBundle] pathForResource:@"AnimatedPaster" ofType:@"bundle"];
  112. }
  113. break;
  114. case PasterType_static:
  115. {
  116. _boundPath = [[NSBundle mainBundle] pathForResource:@"Paster" ofType:@"bundle"];
  117. }
  118. break;
  119. case PasterType_Qipao:
  120. {
  121. _boundPath = [[NSBundle mainBundle] pathForResource:@"bubbleText" ofType:@"bundle"];
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. NSString *jsonString = [NSString stringWithContentsOfFile:[_boundPath stringByAppendingPathComponent:@"config.json"] encoding:NSUTF8StringEncoding error:nil];
  128. NSDictionary *dic = [self dictionaryWithJsonString:jsonString];
  129. _pasterList = dic[@"pasterList"];
  130. int column = 4; //默认4列
  131. CGFloat btnWidth = 70 * kScaleX;
  132. CGFloat space = (self.width - btnWidth *column) / (column + 1);
  133. _selectView.contentSize = CGSizeMake(self.width, (_pasterList.count + 3) / 4 * (btnWidth + space));
  134. [_selectView removeAllSubViews];
  135. for (int i = 0; i < _pasterList.count; i ++) {
  136. NSString *qipaoIconPath = [_boundPath stringByAppendingPathComponent:_pasterList[i][@"icon"]];
  137. UIImage *qipaoIconImage = [UIImage imageWithContentsOfFile:qipaoIconPath];
  138. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  139. [btn setFrame:CGRectMake(space + i % column * (btnWidth + space),space + i / column * (btnWidth + space), btnWidth, btnWidth)];
  140. [btn setImage:qipaoIconImage forState:UIControlStateNormal];
  141. [btn addTarget:self action:@selector(selectBubble:) forControlEvents:UIControlEventTouchUpInside];
  142. btn.tag = i;
  143. [_selectView addSubview:btn];
  144. }
  145. }
  146. - (void)selectBubble:(UIButton *)btn
  147. {
  148. switch (_pasterType) {
  149. case PasterType_Qipao:
  150. {
  151. NSString *qipaoPath = [_boundPath stringByAppendingPathComponent:_pasterList[btn.tag][@"name"]];
  152. NSString *jsonString = [NSString stringWithContentsOfFile:[qipaoPath stringByAppendingPathComponent:@"config.json"] encoding:NSUTF8StringEncoding error:nil];
  153. NSDictionary *dic = [self dictionaryWithJsonString:jsonString];
  154. PasterQipaoInfo *info = [PasterQipaoInfo new];
  155. info.image = [UIImage imageNamed:[qipaoPath stringByAppendingPathComponent:dic[@"name"]]];
  156. info.width = [dic[@"width"] floatValue];
  157. info.height = [dic[@"height"] floatValue];
  158. info.textTop = [dic[@"textTop"] floatValue];
  159. info.textLeft = [dic[@"textLeft"] floatValue];
  160. info.textRight = [dic[@"textRight"] floatValue];
  161. info.textBottom = [dic[@"textBottom"] floatValue];
  162. info.iconImage = btn.imageView.image;
  163. [self.delegate onPasterQipaoSelect:info];
  164. }
  165. break;
  166. case PasterType_Animate:
  167. {
  168. NSString *pasterPath = [_boundPath stringByAppendingPathComponent:_pasterList[btn.tag][@"name"]];
  169. NSString *jsonString = [NSString stringWithContentsOfFile:[pasterPath stringByAppendingPathComponent:@"config.json"] encoding:NSUTF8StringEncoding error:nil];
  170. NSDictionary *dic = [self dictionaryWithJsonString:jsonString];
  171. NSArray *imagePathList = dic[@"frameArry"];
  172. NSMutableArray *imageList = [NSMutableArray array];
  173. for (NSDictionary *dic in imagePathList) {
  174. NSString *imageName = dic[@"picture"];
  175. UIImage *image = [UIImage imageNamed:[pasterPath stringByAppendingPathComponent:imageName]];
  176. [imageList addObject:image];
  177. }
  178. PasterAnimateInfo *info = [PasterAnimateInfo new];
  179. info.imageList = imageList;
  180. info.path = pasterPath;
  181. info.width = [dic[@"width"] floatValue];
  182. info.height = [dic[@"height"] floatValue];
  183. info.duration = [dic[@"period"] floatValue] / 1000.0;
  184. info.iconImage = btn.imageView.image;
  185. [self.delegate onPasterAnimateSelect:info];
  186. }
  187. break;
  188. case PasterType_static:
  189. {
  190. NSString *pasterPath = [_boundPath stringByAppendingPathComponent:_pasterList[btn.tag][@"name"]];
  191. NSString *jsonString = [NSString stringWithContentsOfFile:[pasterPath stringByAppendingPathComponent:@"config.json"] encoding:NSUTF8StringEncoding error:nil];
  192. NSDictionary *dic = [self dictionaryWithJsonString:jsonString];
  193. PasterStaticInfo *info = [PasterStaticInfo new];
  194. info.image = [UIImage imageNamed:[pasterPath stringByAppendingPathComponent:dic[@"name"]]];
  195. info.width = [dic[@"width"] floatValue];
  196. info.height = [dic[@"height"] floatValue];
  197. info.iconImage = btn.imageView.image;
  198. [self.delegate onPasterStaticSelect:info];
  199. }
  200. break;
  201. default:
  202. break;
  203. }
  204. self.hidden = YES;
  205. }
  206. - (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
  207. if (jsonString == nil) {
  208. return nil;
  209. }
  210. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  211. NSError *err;
  212. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  213. options:NSJSONReadingMutableContainers
  214. error:&err];
  215. if(err) {
  216. NSLog(@"json解析失败:%@",err);
  217. return nil;
  218. }
  219. return dic;
  220. }
  221. @end