UGCKitPasterAddView.m 11 KB

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