MHMakeUpView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // MHMakeUpView.m
  3. // TXLiteAVDemo_UGC
  4. //
  5. // Created by Apple on 2021/5/7.
  6. // Copyright © 2021 Tencent. All rights reserved.
  7. //
  8. #import "MHMakeUpView.h"
  9. #import "MHBeautyMenuCell.h"
  10. #import "MHBeautyParams.h"
  11. #import "MHBeautiesModel.h"
  12. #import "WNSegmentControl.h"
  13. #import "MHBottomView.h"
  14. @interface MHMakeUpView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  15. @property (nonatomic, strong) UICollectionView *collectionView;
  16. @property (nonatomic, strong) NSMutableArray *array;
  17. @property (nonatomic, assign) NSInteger lastIndex;
  18. @property (nonatomic, assign) NSInteger beautyType;
  19. @property (nonatomic, strong) WNSegmentControl *segmentControl;
  20. @property (nonatomic, strong) UIView *lineView;
  21. @property (nonatomic, strong) MHBottomView * bottomView;
  22. @end
  23. @implementation MHMakeUpView
  24. - (instancetype)initWithFrame:(CGRect)frame {
  25. if (self = [super initWithFrame:frame]) {
  26. [self addSubview:self.segmentControl];
  27. [self addSubview:self.lineView];
  28. [self addSubview:self.collectionView];
  29. if (isNeedBottom) {
  30. [self addSubview:self.bottomView];
  31. }
  32. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:MHBlackAlpha];
  33. self.lastIndex = -1;
  34. }
  35. return self;
  36. }
  37. - (void)clearAllMakeupEffects {
  38. for (int i = 0; i<self.array.count; i++) {
  39. MHBeautiesModel * model = self.array[i];
  40. model.isSelected = i == 0;
  41. NSString *makeupKey = [NSString stringWithFormat:@"kMHMakeup_%ld",model.type];
  42. [[NSUserDefaults standardUserDefaults] setBool:model.isSelected forKey:makeupKey];
  43. }
  44. [self.collectionView reloadData];
  45. }
  46. #pragma mark - 底部按钮响应
  47. - (void)cameraAction:(BOOL)isTakePhoto{
  48. NSLog(@"点击了拍照");
  49. if (isTakePhoto) {
  50. if ([self.delegate respondsToSelector:@selector(takePhoto)]) {
  51. [self.delegate takePhoto];
  52. }
  53. }else{
  54. if ([self.delegate respondsToSelector:@selector(clickPackUp)]) {
  55. [self.delegate clickPackUp];
  56. }
  57. }
  58. }
  59. #pragma mark - collectionView
  60. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  61. return self.array.count;
  62. }
  63. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  64. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHBeautyMenuCell" forIndexPath:indexPath];
  65. cell.beautyModel = self.array[indexPath.row];
  66. return cell;
  67. }
  68. - (CGSize)collectionView:(UICollectionView *)collectionView
  69. layout:(UICollectionViewLayout *)collectionViewLayout
  70. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  71. return CGSizeMake((window_width-40)/4, MHMeiyanMenusCellHeight);
  72. }
  73. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  74. if (self.lastIndex == indexPath.row) {
  75. return;
  76. }
  77. MHBeautiesModel *currentModel = self.array[indexPath.row];
  78. currentModel.isSelected = !currentModel.isSelected;
  79. if (indexPath.row == 0) {
  80. [self clearAllMakeupEffects];
  81. }else{
  82. MHBeautiesModel *model = self.array[0];
  83. model.isSelected = NO;
  84. [[NSUserDefaults standardUserDefaults] setBool:model.isSelected forKey:@"kMHMakeup_0"];
  85. }
  86. self.lastIndex = indexPath.row;
  87. [self.collectionView reloadData];
  88. self.beautyType = currentModel.type;
  89. if (isSaveMakeup) {
  90. if (self.beautyType != 0) {
  91. NSString *makeupKey = [NSString stringWithFormat:@"kMHMakeup_%ld",(long)self.beautyType];
  92. [[NSUserDefaults standardUserDefaults] setBool:currentModel.isSelected forKey:makeupKey];
  93. }
  94. }
  95. if ([self.delegate respondsToSelector:@selector(handleMakeUpType:withON:)]) {
  96. [self.delegate handleMakeUpType:currentModel.type withON:currentModel.isSelected];
  97. }
  98. }
  99. #pragma mark - lazy
  100. - (NSMutableArray *)array {
  101. if (!_array) {
  102. NSMutableArray * selectedItemArray = [MHSDK shareInstance].makeupArray;
  103. NSString *path = [[NSBundle mainBundle] pathForResource:@"MHMakeupParams" ofType:@"plist"];
  104. NSArray *items = [NSArray arrayWithContentsOfFile:path];
  105. NSMutableArray * selectedItems = [NSMutableArray array];
  106. for (int i = 0; i < selectedItemArray.count; i ++) {
  107. NSDictionary * selectedItemDic = selectedItemArray[i];
  108. NSString * selectedName = selectedItemDic[@"name"];
  109. for (int j = 0; j < items.count; j++) {
  110. NSDictionary * itemDic = items[j];
  111. NSString * itemName = itemDic[@"name"];
  112. if ([selectedName isEqual:itemName]) {
  113. [selectedItems addObject:itemDic];
  114. }
  115. }
  116. }
  117. _array = [NSMutableArray array];
  118. for (int i = 0; i<selectedItems.count; i++) {
  119. NSDictionary * itemDic = selectedItems[i];
  120. MHBeautiesModel *model = [[MHBeautiesModel alloc] init];
  121. model.imgName = itemDic[@"imageName"];
  122. model.beautyTitle = itemDic[@"name"];
  123. model.menuType = MHBeautyMenuType_MakeUp;
  124. model.type = [itemDic[@"type"] integerValue];
  125. if (isSaveMakeup) {
  126. NSString *makeUpKey = [NSString stringWithFormat:@"kMHMakeup_%ld",model.type];
  127. BOOL isSelected = [[NSUserDefaults standardUserDefaults] boolForKey:makeUpKey];
  128. model.isSelected = isSelected;
  129. }else{
  130. model.isSelected = i==0;
  131. }
  132. [_array addObject:model];
  133. }
  134. }
  135. return _array;
  136. }
  137. - (UICollectionView *)collectionView {
  138. if (!_collectionView) {
  139. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  140. layout.minimumLineSpacing = 0;
  141. layout.minimumInteritemSpacing = 10;
  142. layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
  143. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  144. ///修改MHUI
  145. CGFloat bottom = _lineView.frame.origin.y + _lineView.frame.size.height;
  146. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, bottom, window_width, self.frame.size.height - bottom - MHBottomViewHeight) collectionViewLayout:layout];
  147. ///修改MHUI
  148. _collectionView.backgroundColor = [UIColor clearColor];
  149. _collectionView.delegate = self;
  150. _collectionView.dataSource = self;
  151. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHBeautyMenuCell"];
  152. _collectionView.showsHorizontalScrollIndicator = NO;
  153. }
  154. return _collectionView;
  155. }
  156. ///修改MHUI
  157. - (WNSegmentControl *)segmentControl {
  158. if (!_segmentControl) {
  159. _segmentControl = [[WNSegmentControl alloc] initWithTitles:@[@"美妆"]];
  160. _segmentControl.frame = CGRectMake(0, 0, window_width, MHStickerSectionHeight);
  161. _segmentControl.backgroundColor = [UIColor clearColor];
  162. [_segmentControl setTextAttributes:@{NSFontAttributeName: Font_12, NSForegroundColorAttributeName: FontColorNormal}
  163. forState:UIControlStateNormal];
  164. [_segmentControl setTextAttributes:@{NSFontAttributeName: Font_12, NSForegroundColorAttributeName: FontColorSelected}
  165. forState:UIControlStateSelected];
  166. _segmentControl.selectedSegmentIndex = 0;
  167. _segmentControl.widthStyle = WNSegmentedControlWidthStyleFixed;
  168. // [_segmentControl addTarget:self action:@selector(switchList:) forControlEvents:UIControlEventValueChanged];
  169. }
  170. return _segmentControl;
  171. }
  172. - (UIView *)lineView {
  173. if (!_lineView) {
  174. CGFloat bottom = _segmentControl.frame.origin.y + _segmentControl.frame.size.height;
  175. _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, bottom, window_width, 0.5)];
  176. _lineView.backgroundColor = LineColor;
  177. }
  178. return _lineView;
  179. }
  180. ///修改MHUI
  181. - (MHBottomView*)bottomView{
  182. if (!_bottomView) {
  183. __weak typeof(self) weakSelf = self;
  184. CGFloat bottom = self.collectionView.frame.origin.y + self.collectionView.frame.size.height;
  185. _bottomView = [[MHBottomView alloc] initWithFrame:CGRectMake(0, bottom, window_width, MHBottomViewHeight)];
  186. _bottomView.clickBtn = ^(BOOL isTakePhoto) {
  187. [weakSelf cameraAction:isTakePhoto];
  188. };
  189. }
  190. return _bottomView;
  191. }
  192. - (NSInteger)currentIndex{
  193. return _lastIndex;
  194. }
  195. @end