TiUIMainMenuView.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //
  2. // TiUIMainMenuView.m
  3. // TiSDKDemo
  4. //
  5. // Created by iMacA1002 on 2019/12/2.
  6. // Copyright © 2020 Tillusory Tech. All rights reserved.
  7. //
  8. #import "TiUIMainMenuView.h"
  9. #import "TIConfig.h"
  10. #import "TISetSDKParameters.h"
  11. #import "TIDownloadZipManager.h"
  12. #import "TiUISliderRelatedView.h"
  13. #import "TiUIMenuViewCell.h"
  14. #import "TiUIMenuOneViewCell.h"
  15. #import "TiUIMenuTowViewCell.h"
  16. #import "TiUIMenuThreeViewCell.h"
  17. @interface TiUIMainMenuView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  18. //滑块相关View
  19. @property(nonatomic,strong) TiUISliderRelatedView *sliderRelatedView;
  20. //菜单view背景
  21. @property(nonatomic,strong) UIView *backgroundView;
  22. //美颜菜单view
  23. @property(nonatomic,strong) UICollectionView *menuView;
  24. //美颜菜单二级联动CollectionView子菜单
  25. @property(nonatomic,strong) UICollectionView *subMenuView;
  26. @property(nonatomic,strong) NSIndexPath *selectedIndexPath;
  27. @property(nonatomic,assign) NSInteger mainindex;
  28. @property(nonatomic,assign) NSInteger subindex;
  29. @end
  30. static NSString *const TiUIMenuViewCollectionViewCellId = @"TiUIMainMenuViewCollectionViewCellId";
  31. static NSString *const TiUISubMenuViewCollectionViewCellId = @"TiUIMainSubMenuViewCollectionViewCellId";
  32. @implementation TiUIMainMenuView
  33. -(TiUISliderRelatedView *)sliderRelatedView{
  34. if (_sliderRelatedView == nil) {
  35. _sliderRelatedView = [[TiUISliderRelatedView alloc]init];
  36. //默认美白拉条
  37. [_sliderRelatedView.sliderView setSliderType:TI_UI_SLIDER_TYPE_ONE WithValue:[TISetSDKParameters getFloatValueForKey:TI_UIDCK_SKIN_WHITENING_SLIDER]];
  38. WeakSelf;//滑动拉条调用成回调
  39. [_sliderRelatedView.sliderView setRefreshValueBlock:^(CGFloat value) {
  40. TiUIDataCategoryKey valueForKey;
  41. if (weakSelf.mainindex==4) {
  42. valueForKey = TI_UIDCK_FILTER_SLIDER;
  43. }else{
  44. valueForKey = (weakSelf.mainindex+1)*100 + weakSelf.subindex;
  45. }
  46. //储存滑条参数
  47. [TISetSDKParameters setFloatValue:value forKey:valueForKey];
  48. //设置美颜参数
  49. [TISetSDKParameters setBeautySlider:value forKey:valueForKey withIndex:weakSelf.subindex];
  50. }];
  51. }
  52. return _sliderRelatedView;
  53. }
  54. -(UIView *)backgroundView
  55. {
  56. if (_backgroundView == nil) {
  57. _backgroundView = [[UIView alloc]init];
  58. _backgroundView.backgroundColor = TI_Color_Default_Text_Black;
  59. }
  60. return _backgroundView;
  61. }
  62. -(UICollectionView *)menuView{
  63. if (_menuView == nil) {
  64. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  65. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  66. layout.itemSize = CGSizeMake(TiUIMenuViewHeight*2, TiUIMenuViewHeight);
  67. // // 设置最小行间距
  68. layout.minimumLineSpacing = 0;
  69. _menuView =[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  70. [_menuView setTag:10];
  71. _menuView.showsHorizontalScrollIndicator = NO;
  72. _menuView.backgroundColor=[UIColor whiteColor];
  73. _menuView.dataSource= self;
  74. _menuView.delegate = self;
  75. [_menuView registerClass:[TiUIMenuViewCell class] forCellWithReuseIdentifier:TiUIMenuViewCollectionViewCellId];
  76. }
  77. return _menuView;
  78. }
  79. -(UICollectionView *)subMenuView{
  80. if (_subMenuView == nil) {
  81. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  82. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  83. // SCREEN_WIDTH
  84. layout.itemSize = CGSizeMake(self.frame.size.width, TiUIViewBoxTotalHeight- TiUIMenuViewHeight - TiUISliderRelatedViewHeight-1);
  85. // // 设置最小行间距
  86. layout.minimumLineSpacing = 0;
  87. _subMenuView =[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  88. [_subMenuView setTag:20];
  89. _subMenuView.showsHorizontalScrollIndicator = NO;
  90. _subMenuView.backgroundColor=[UIColor whiteColor];
  91. _subMenuView.dataSource= self;
  92. _subMenuView.scrollEnabled = NO;//禁止滑动
  93. //注册多个cell 不重用,重用会导致嵌套的UICollectionView内的cell 错乱
  94. // FIXME: --json 数据完善后可再次尝试--
  95. for (TIMenuMode *mod in [TIMenuPlistManager shareManager].mainModeArr) {
  96. switch (mod.menuTag) {
  97. case 0:
  98. case 1:
  99. {
  100. [_subMenuView registerClass:[TiUIMenuOneViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mod.menuTag]];
  101. }
  102. break;
  103. case 4:
  104. case 5:
  105. case 6:
  106. {
  107. [_subMenuView registerClass:[TiUIMenuTowViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mod.menuTag]];
  108. }
  109. break;
  110. case 2:
  111. case 3:
  112. case 7:
  113. case 8:
  114. case 9:
  115. {
  116. [_subMenuView registerClass:[TiUIMenuThreeViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mod.menuTag]];
  117. }
  118. break;
  119. default:
  120. {
  121. [_subMenuView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mod.menuTag]];
  122. }
  123. break;
  124. }
  125. }
  126. }
  127. return _subMenuView;
  128. }
  129. - (instancetype)initWithFrame:(CGRect)frame
  130. {
  131. self = [super initWithFrame:frame];
  132. if (self) {
  133. [self addSubview:self.sliderRelatedView];
  134. [self addSubview:self.backgroundView];
  135. [self.backgroundView addSubview:self.menuView];
  136. [self.backgroundView addSubview:self.subMenuView];
  137. [self.sliderRelatedView mas_makeConstraints:^(MASConstraintMaker *make) {
  138. make.left.top.right.equalTo(self);
  139. make.height.mas_offset(TiUISliderRelatedViewHeight);
  140. }];
  141. [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
  142. make.left.right.bottom.equalTo(self);
  143. make.top.equalTo(self.sliderRelatedView.mas_bottom);
  144. }];
  145. [self.menuView mas_makeConstraints:^(MASConstraintMaker *make) {
  146. make.left.right.top.equalTo(self.backgroundView);
  147. make.height.mas_offset(TiUIMenuViewHeight);
  148. }];
  149. [self.subMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.left.right.bottom.equalTo(self.backgroundView);
  151. make.top.equalTo(self.menuView.mas_bottom).offset(0.5);
  152. }];
  153. }
  154. return self;
  155. }
  156. #pragma mark ---UICollectionViewDataSource---
  157. //设置每个section包含的item数目
  158. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  159. return [[TIMenuPlistManager shareManager] mainModeArr].count;
  160. }
  161. //返回对应indexPath的cell
  162. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  163. TIMenuMode *mode = [[TIMenuPlistManager shareManager] mainModeArr][indexPath.row];
  164. if (collectionView.tag==10) {
  165. TiUIMenuViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:TiUIMenuViewCollectionViewCellId forIndexPath:indexPath];
  166. if (mode.selected)
  167. {
  168. self.selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  169. }
  170. [cell setMenuMode:mode];
  171. return cell;
  172. }else if (collectionView.tag==20){
  173. switch (mode.menuTag) {
  174. case 0:
  175. case 1:
  176. {
  177. TiUIMenuOneViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mode.menuTag] forIndexPath:indexPath];
  178. WeakSelf;
  179. [cell setClickOnCellBlock:^(NSInteger index) {
  180. weakSelf.subindex = index;
  181. [weakSelf setSliderTypeAndValue];
  182. }];
  183. [cell setMode:mode];
  184. return cell;
  185. }
  186. break;
  187. case 4:
  188. case 5:
  189. case 6:
  190. {
  191. TiUIMenuTowViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mode.menuTag] forIndexPath:indexPath];
  192. WeakSelf;
  193. [cell setClickOnCellBlock:^(NSInteger index) {//只有滤镜执行
  194. weakSelf.subindex = index;
  195. [weakSelf setSliderTypeAndValue];
  196. }];
  197. [cell setMode:mode];
  198. return cell;
  199. }
  200. break;
  201. case 2:
  202. case 3:
  203. case 7:
  204. case 8:
  205. case 9:
  206. {
  207. TiUIMenuThreeViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mode.menuTag] forIndexPath:indexPath];
  208. [cell setMode:mode];
  209. return cell;
  210. }
  211. break;
  212. default:
  213. {
  214. UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%ld",TiUISubMenuViewCollectionViewCellId,(long)mode.menuTag] forIndexPath:indexPath];
  215. cell.backgroundColor = [UIColor orangeColor];
  216. return cell;
  217. }
  218. break;
  219. }
  220. }
  221. return nil;
  222. }
  223. #pragma mark ---UICollectionViewDelegate---
  224. //选择了某个cell
  225. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  226. if(indexPath.row == self.selectedIndexPath.row) return;
  227. if (collectionView.tag ==10)
  228. {
  229. self.mainindex = indexPath.row;
  230. switch (indexPath.row) {
  231. case 0:
  232. for (TIMenuMode *mod in [TIMenuPlistManager shareManager].meiyanModeArr) {
  233. if (mod.selected) {
  234. self.subindex = mod.menuTag;
  235. }
  236. }
  237. [self.sliderRelatedView setSliderHidden:NO];
  238. [self setSliderTypeAndValue];
  239. break;
  240. case 1:
  241. for (TIMenuMode *mod in [TIMenuPlistManager shareManager].meixingModeArr) {
  242. if (mod.selected) {
  243. self.subindex = mod.menuTag;
  244. }
  245. }
  246. [self.sliderRelatedView setSliderHidden:NO];
  247. [self setSliderTypeAndValue];
  248. break;
  249. case 4:
  250. for (TIMenuMode *mod in [TIMenuPlistManager shareManager].lvjingModeArr) {
  251. if (mod.selected) {
  252. self.subindex = mod.menuTag;
  253. }
  254. }
  255. [self.sliderRelatedView setSliderHidden:NO];
  256. [self setSliderTypeAndValue];
  257. break;
  258. break;
  259. default:
  260. [self.sliderRelatedView setSliderHidden:YES];
  261. break;
  262. }
  263. [TIMenuPlistManager shareManager].mainModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(YES) forKey:@"selected" In:indexPath.row WithPath:@"TIMenu.json"];
  264. [TIMenuPlistManager shareManager].mainModeArr = [[TIMenuPlistManager shareManager] modifyObject:@(NO) forKey:@"selected" In:self.selectedIndexPath.row WithPath:@"TIMenu.json"];
  265. if(self.selectedIndexPath){
  266. [collectionView reloadItemsAtIndexPaths:@[self.selectedIndexPath,indexPath]];
  267. }
  268. else
  269. {
  270. [collectionView reloadItemsAtIndexPaths:@[indexPath]];
  271. }
  272. self.selectedIndexPath = indexPath;
  273. [self.subMenuView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
  274. }
  275. }
  276. -(void)setSliderTypeAndValue{
  277. TiUISliderType sliderType = TI_UI_SLIDER_TYPE_ONE;
  278. TiUIDataCategoryKey categoryKey = TI_UIDCK_SKIN_WHITENING_SLIDER;
  279. if (self.mainindex==0) {
  280. switch (self.subindex) {
  281. case 0:
  282. sliderType = TI_UI_SLIDER_TYPE_ONE;
  283. categoryKey = TI_UIDCK_SKIN_WHITENING_SLIDER;// 美白
  284. break;
  285. case 1:
  286. sliderType = TI_UI_SLIDER_TYPE_ONE;
  287. categoryKey = TI_UIDCK_SKIN_BLEMISH_REMOVAL_SLIDER;// 磨皮
  288. break;
  289. case 2:
  290. sliderType = TI_UI_SLIDER_TYPE_TWO;
  291. categoryKey = TI_UIDCK_SKIN_BRIGHTNESS_SLIDER;// 亮度
  292. break;
  293. case 3:
  294. sliderType = TI_UI_SLIDER_TYPE_ONE;
  295. categoryKey = TI_UIDCK_SKIN_TENDERNESS_SLIDER;// 粉嫩
  296. break;
  297. case 4:
  298. sliderType = TI_UI_SLIDER_TYPE_ONE;
  299. categoryKey = TI_UIDCK_SKIN_SKINBRIGGT_SLIDER;// 鲜明
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. else if (self.mainindex==1)
  306. {
  307. switch (self.subindex) {
  308. case 0:
  309. sliderType = TI_UI_SLIDER_TYPE_ONE;
  310. categoryKey = TI_UIDCK_EYE_MAGNIFYING_SLIDER;// 大眼
  311. break;
  312. case 1:
  313. sliderType = TI_UI_SLIDER_TYPE_ONE;
  314. categoryKey = TI_UIDCK_FACE_NARROWING_SLIDER;// 瘦脸
  315. break;
  316. case 2:
  317. sliderType = TI_UI_SLIDER_TYPE_ONE;
  318. categoryKey = TI_UIDCK_CHIN_SLIMMING_SLIDER;// 窄脸
  319. break;
  320. case 3:
  321. sliderType = TI_UI_SLIDER_TYPE_TWO;
  322. categoryKey = TI_UIDCK_JAW_TRANSFORMING_SLIDER;// 下巴
  323. break;
  324. case 4:
  325. sliderType = TI_UI_SLIDER_TYPE_TWO;
  326. categoryKey = TI_UIDCK_FOREHEAD_TRANSFORMING_SLIDER;// 额头
  327. break;
  328. case 5:
  329. sliderType = TI_UI_SLIDER_TYPE_TWO;
  330. categoryKey = TI_UIDCK_MOUTH_TRANSFORMING_SLIDER;// 嘴型
  331. break;
  332. case 6:
  333. sliderType = TI_UI_SLIDER_TYPE_TWO;
  334. categoryKey = TI_UIDCK_NOSE_SLIMMING_SLIDER;// 瘦鼻
  335. break;
  336. case 7:
  337. sliderType = TI_UI_SLIDER_TYPE_ONE;
  338. categoryKey = TI_UIDCK_TEETH_WHITENING_SLIDER;// 美牙
  339. break;
  340. case 8:
  341. sliderType = TI_UI_SLIDER_TYPE_TWO;
  342. categoryKey = TI_UIDCK_EYE_SPACING_SLIDER;// 眼间距
  343. break;
  344. case 9:
  345. sliderType = TI_UI_SLIDER_TYPE_ONE;
  346. categoryKey = TI_UIDCK_NOSE_LONG_SLIDER;// 长鼻
  347. break;
  348. case 10:
  349. sliderType = TI_UI_SLIDER_TYPE_TWO;
  350. categoryKey = TI_UIDCK_EYE_CORNER_SLIDER;// 眼角
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. else if (self.mainindex==4)
  357. {
  358. sliderType = TI_UI_SLIDER_TYPE_ONE;
  359. categoryKey = TI_UIDCK_FILTER_SLIDER;// 滤镜
  360. if (self.subindex) {
  361. [self.sliderRelatedView setSliderHidden:NO];
  362. }else{
  363. [self.sliderRelatedView setSliderHidden:YES];
  364. }
  365. }
  366. [self.sliderRelatedView.sliderView setSliderType:sliderType WithValue:[TISetSDKParameters getFloatValueForKey:categoryKey]];
  367. }
  368. -(void)dealloc{
  369. [TIMenuPlistManager releaseShareManager];
  370. [TIDownloadZipManager releaseShareManager];
  371. }
  372. @end