TiUIMenuOneViewCell.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // TiUISubMenuOneViewCell.m
  3. // TiSDKDemo
  4. //
  5. // Created by iMacA1002 on 2019/12/5.
  6. // Copyright © 2020 Tillusory Tech. All rights reserved.
  7. //
  8. #import "TiUIMenuOneViewCell.h"
  9. #import "TIConfig.h"
  10. #import "TIButton.h"
  11. #import "TiUISubMenuOneViewCell.h"
  12. @interface TiUIMenuOneViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
  13. @property(nonatomic,strong)TIButton *totalSwitch;
  14. @property(nonatomic,strong)UIView *lineView;
  15. @property(nonatomic,strong) UICollectionView *menuCollectionView;
  16. @property(nonatomic,strong) NSIndexPath *selectedIndexPath;
  17. @end
  18. static NSString *const TiUIMenuCollectionViewCellId = @"TiUIMainMenuCollectionViewnOneCellId";
  19. @implementation TiUIMenuOneViewCell
  20. -(TIButton *)totalSwitch{
  21. if (_totalSwitch==nil) {
  22. _totalSwitch = [[TIButton alloc]initWithScaling:0.9];
  23. [_totalSwitch addTarget:self action:@selector(totalSwitch:) forControlEvents:UIControlEventTouchUpInside];
  24. }
  25. return _totalSwitch;
  26. }
  27. -(UIView *)lineView{
  28. if (_lineView==nil) {
  29. _lineView = [[UIView alloc]init];
  30. _lineView.backgroundColor = TI_Color_Default_Text_Black;
  31. }
  32. return _lineView;
  33. }
  34. -(UICollectionView *)menuCollectionView{
  35. if (_menuCollectionView == nil) {
  36. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  37. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  38. layout.itemSize = CGSizeMake(TiUISubMenuOneViewTIButtonWidth, TiUISubMenuOneViewTIButtonHeight);
  39. // // 设置最小行间距
  40. layout.minimumLineSpacing = 15;
  41. _menuCollectionView =[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  42. _menuCollectionView.showsHorizontalScrollIndicator = NO;
  43. _menuCollectionView.backgroundColor = [UIColor whiteColor];
  44. _menuCollectionView.dataSource= self;
  45. _menuCollectionView.delegate = self;
  46. [_menuCollectionView registerClass:[TiUISubMenuOneViewCell class] forCellWithReuseIdentifier:TiUIMenuCollectionViewCellId];
  47. }
  48. return _menuCollectionView;
  49. }
  50. - (instancetype)initWithFrame:(CGRect)frame
  51. {
  52. self = [super initWithFrame:frame];
  53. if (self) {
  54. [self addSubview:self.totalSwitch];
  55. [self addSubview:self.lineView];
  56. [self addSubview:self.menuCollectionView];
  57. [self.totalSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.centerY.equalTo(self.mas_centerY);
  59. make.left.equalTo(self.mas_left).offset(35);
  60. make.width.mas_equalTo(TiUISubMenuOneViewTIButtonWidth-12);
  61. make.height.mas_equalTo(TiUISubMenuOneViewTIButtonHeight);
  62. }];
  63. [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.centerY.equalTo(self.mas_centerY);
  65. make.left.equalTo(self.totalSwitch.mas_right).offset(20);
  66. make.width.mas_equalTo(0.5);
  67. make.height.mas_equalTo(TiUISubMenuOneViewTIButtonHeight);
  68. }];
  69. [self.menuCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.left.equalTo(self.lineView.mas_right).offset(25);
  71. make.right.equalTo(self.mas_right).offset(-20);
  72. make.centerY.equalTo(self.mas_centerY);
  73. make.height.mas_equalTo(TiUISubMenuOneViewTIButtonHeight);
  74. }];
  75. }
  76. return self;
  77. }
  78. #pragma mark ---UICollectionViewDataSource---
  79. //设置每个section包含的item数目
  80. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  81. switch (self.mode.menuTag) {
  82. case 0:
  83. return [TIMenuPlistManager shareManager].meiyanModeArr.count;
  84. break;
  85. case 1:
  86. return [TIMenuPlistManager shareManager].meixingModeArr.count;
  87. break;
  88. default:
  89. return 0;
  90. break;
  91. }
  92. }
  93. //返回对应indexPath的cell
  94. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  95. TiUISubMenuOneViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:TiUIMenuCollectionViewCellId forIndexPath:indexPath];
  96. switch (self.mode.menuTag) {
  97. case 0:
  98. {
  99. TIMenuMode * subMod = [[TIMenuPlistManager shareManager].meiyanModeArr objectAtIndex:indexPath.row];
  100. if (subMod.selected)
  101. {
  102. self.selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  103. }
  104. [cell setSubMod:subMod];
  105. }
  106. break;
  107. case 1:
  108. {
  109. TIMenuMode * subMod = [[TIMenuPlistManager shareManager].meixingModeArr objectAtIndex:indexPath.row];
  110. if (subMod.selected)
  111. {
  112. self.selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  113. }
  114. [cell setSubMod:subMod];
  115. }
  116. break;
  117. case 6:
  118. {
  119. TIMenuMode * subMod = [[TIMenuPlistManager shareManager].hahajingModeArr objectAtIndex:indexPath.row];
  120. if (subMod.selected)
  121. {
  122. self.selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  123. }
  124. [cell setSubMod:subMod];
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. return cell;
  131. }
  132. #pragma mark ---UICollectionViewDelegate---
  133. //选择了某个cell
  134. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  135. if(indexPath.row == self.selectedIndexPath.row) return;
  136. if (self.clickOnCellBlock)
  137. {
  138. self.clickOnCellBlock(indexPath.row);
  139. }
  140. switch (self.mode.menuTag) {
  141. case 0:
  142. {
  143. [TIMenuPlistManager shareManager].meiyanModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(YES) forKey:@"selected" In:indexPath.row WithPath:@"TIMeiYanMenu.json"];
  144. [TIMenuPlistManager shareManager].meiyanModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(NO) forKey:@"selected" In:self.selectedIndexPath.row WithPath:@"TIMeiYanMenu.json"];
  145. if (self.selectedIndexPath) {
  146. [collectionView reloadItemsAtIndexPaths:@[self.selectedIndexPath,indexPath]];
  147. }else{
  148. [collectionView reloadItemsAtIndexPaths:@[indexPath]];
  149. }
  150. self.selectedIndexPath = indexPath;
  151. }
  152. break;
  153. case 1:
  154. {
  155. [TIMenuPlistManager shareManager].meixingModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(YES) forKey:@"selected" In:indexPath.row WithPath:@"TIMeiXingMenu.json"];
  156. [TIMenuPlistManager shareManager].meixingModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(NO) forKey:@"selected" In:self.selectedIndexPath.row WithPath:@"TIMeiXingMenu.json"];
  157. if (self.selectedIndexPath) {
  158. [collectionView reloadItemsAtIndexPaths:@[self.selectedIndexPath,indexPath]];
  159. }else{
  160. [collectionView reloadItemsAtIndexPaths:@[indexPath]];
  161. }
  162. self.selectedIndexPath = indexPath;
  163. }
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. -(void)totalSwitch:(TIButton *)sender
  170. {
  171. [TIMenuPlistManager shareManager].mainModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(!self.mode.totalSwitch) forKey:@"totalSwitch" In:self.mode.menuTag WithPath:@"TIMenu.json"];
  172. TIMenuMode *newMod = [TIMenuPlistManager shareManager].mainModeArr[self.mode.menuTag];
  173. _mode = newMod;
  174. [self.totalSwitch setSelected:newMod.totalSwitch];
  175. if (newMod.menuTag ==0)
  176. {
  177. [[TiSDKManager shareManager] setBeautyEnable:newMod.totalSwitch];
  178. }
  179. else if(newMod.menuTag==1)
  180. {
  181. [[TiSDKManager shareManager] setFaceTrimEnable:newMod.totalSwitch];
  182. }
  183. }
  184. - (void)setMode:(TIMenuMode *)mode{
  185. if (mode) {
  186. _mode = mode;
  187. [self.totalSwitch setTitle:[NSString stringWithFormat:ASLocalizedString(@"%@:关"),mode.name] withImage:[UIImage imageNamed:@"btn_close"] withTextColor:TI_Color_Default_Text_Black forState:UIControlStateNormal];
  188. [self.totalSwitch setTitle:[NSString stringWithFormat:ASLocalizedString(@"%@:开"),mode.name] withImage:[UIImage imageNamed:@"btn_open"] withTextColor:TI_Color_Default_Background_Pink forState:UIControlStateSelected];
  189. [self.totalSwitch setSelected:mode.totalSwitch];
  190. }
  191. }
  192. @end