MHBeautyView.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //MHBeautyView.m
  2. //美颜页面
  3. #import "MHBeautyView.h"
  4. #import "MHBeautyMenuCell.h"
  5. #import "MHBeautyParams.h"
  6. #import "MHBeautiesModel.h"
  7. @interface MHBeautyView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  8. @property (nonatomic, strong) UICollectionView *collectionView;
  9. @property (nonatomic, strong) NSMutableArray *array;
  10. @property (nonatomic, assign) NSInteger lastIndex;
  11. @property (nonatomic, assign) NSInteger beautyType;
  12. @property (nonatomic, strong) NSMutableArray *arr;
  13. @end
  14. @implementation MHBeautyView
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. if (self = [super initWithFrame:frame]) {
  17. [self addSubview:self.collectionView];
  18. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:MHBlackAlpha];
  19. NSDictionary *indexDic = [[NSUserDefaults standardUserDefaults] objectForKey:kMHBeautyTitle];
  20. if(IsDictionaryWithAnyKeyValue(indexDic)){
  21. NSNumber *index = indexDic.allValues.firstObject;
  22. if(index){
  23. self.lastIndex = index.integerValue;
  24. }else{
  25. self.lastIndex = -1;
  26. }
  27. }
  28. }
  29. return self;
  30. }
  31. - (void)configureBeautyData:(NSMutableArray *)beautyArr{
  32. self.array = beautyArr;
  33. [self.collectionView reloadData];
  34. }
  35. - (void)clearAllBeautyEffects {
  36. for (int i = 0; i<self.array.count; i++) {
  37. NSString *beautKey = [NSString stringWithFormat:@"beauty_%ld",(long)i];
  38. [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:beautKey];
  39. MHBeautiesModel *model = self.array[i];
  40. model.isSelected = i==0;
  41. }
  42. [self.collectionView reloadData];
  43. }
  44. - (void)cancelSelectedBeautyType:(NSInteger)type {
  45. for (int i = 0; i<self.array.count; i++) {
  46. MHBeautiesModel *model = self.array[i];
  47. if (model.type == type) {
  48. model.isSelected = NO;
  49. }
  50. }
  51. self.lastIndex = -1;
  52. [self.collectionView reloadData];
  53. }
  54. #pragma mark - collectionView
  55. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  56. return self.array.count;
  57. }
  58. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  59. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHBeautyMenuCell" forIndexPath:indexPath];
  60. cell.beautyModel = self.array[indexPath.row];
  61. return cell;
  62. }
  63. - (CGSize)collectionView:(UICollectionView *)collectionView
  64. layout:(UICollectionViewLayout *)collectionViewLayout
  65. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  66. return CGSizeMake((window_width-40)/5, MHMeiyanMenusCellHeight);
  67. }
  68. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  69. if (self.lastIndex == indexPath.row) {
  70. return;
  71. }
  72. MHBeautiesModel *currentModel = self.array[indexPath.row];
  73. currentModel.isSelected = YES;
  74. if (self.lastIndex >= 0) {
  75. MHBeautiesModel *lastModel = self.array[self.lastIndex];
  76. lastModel.isSelected = NO;
  77. }
  78. NSDictionary *indexDic = @{currentModel.beautyTitle:@(indexPath.row)};
  79. [[NSUserDefaults standardUserDefaults] setValue:indexDic forKey:kMHBeautyTitle];
  80. if (indexPath.row == 0) {
  81. [self clearAllBeautyEffects];
  82. }else{
  83. MHBeautiesModel *firstModel = self.array[0];
  84. firstModel.isSelected = NO;
  85. }
  86. self.lastIndex = indexPath.row;
  87. [self.collectionView reloadData];
  88. NSString *beautKey = [NSString stringWithFormat:@"beauty_%ld",(long)currentModel.type];
  89. NSInteger currentValue = [[NSUserDefaults standardUserDefaults] integerForKey:beautKey];
  90. if ([self.delegate respondsToSelector:@selector(handleBeautyEffects:sliderValue:name:)]) {
  91. [self.delegate handleBeautyEffects:currentModel.type sliderValue:currentValue name:currentModel.beautyTitle];
  92. }
  93. }
  94. #pragma mark - lazy
  95. //- (NSMutableArray *)array {
  96. // if (!_array) {
  97. // NSMutableArray * selectedItemArray = [MHSDK shareInstance].skinArray;
  98. // NSString *path = [[NSBundle mainBundle] pathForResource:@"MHBeautyParams" ofType:@"plist"];
  99. // NSArray *items = [NSArray arrayWithContentsOfFile:path];
  100. //
  101. // NSMutableArray * selectedItems = [NSMutableArray array];
  102. // for (int i = 0; i < selectedItemArray.count; i ++) {
  103. // NSDictionary * selectedItemDic = selectedItemArray[i];
  104. // NSString * selectedName = selectedItemDic[@"name"];
  105. // for (int j = 0; j < items.count; j++) {
  106. // NSDictionary * itemDic = items[j];
  107. // NSString * itemName = itemDic[@"name"];
  108. // if ([selectedName isEqual:itemName]) {
  109. // [selectedItems addObject:itemDic];
  110. // }
  111. // }
  112. // }
  113. // _array = [NSMutableArray array];
  114. // NSString *title = [[NSUserDefaults standardUserDefaults] valueForKey:kMHBeautyTitle];
  115. // for (int i = 0; i<selectedItems.count; i++) {
  116. // NSDictionary * itemDic = selectedItems[i];
  117. // MHBeautiesModel *model = [[MHBeautiesModel alloc] init];
  118. // model.imgName = itemDic[@"imageName"];
  119. // model.beautyTitle = itemDic[@"name"];
  120. // model.menuType = MHBeautyMenuType_Beauty;
  121. // model.type = [itemDic[@"type"] integerValue];
  122. // model.originalValue = itemDic[@"value"];
  123. // model.isSelected = [model.beautyTitle isEqualToString:title];
  124. //// NSString *beautKey = [NSString stringWithFormat:@"beauty_%ld",model.type];
  125. //// NSInteger originalValue = [[NSUserDefaults standardUserDefaults] integerForKey:beautKey];
  126. //// if (originalValue > 0) {
  127. //// model.originalValue = [NSString stringWithFormat:@"%ld",originalValue];
  128. //// [[NSUserDefaults standardUserDefaults] setInteger:originalValue forKey:beautKey];
  129. //// }
  130. // [_array addObject:model];
  131. // }
  132. // }
  133. // return _array;
  134. //}
  135. - (UICollectionView *)collectionView {
  136. if (!_collectionView) {
  137. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  138. layout.minimumLineSpacing = 0;
  139. layout.minimumInteritemSpacing = 0;
  140. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  141. layout.sectionInset = UIEdgeInsetsMake(20, 20,20,20);
  142. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width,self.frame.size.height) collectionViewLayout:layout];
  143. _collectionView.backgroundColor = [UIColor clearColor];
  144. _collectionView.delegate = self;
  145. _collectionView.dataSource = self;
  146. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHBeautyMenuCell"];
  147. }
  148. return _collectionView;
  149. }
  150. - (NSInteger)currentIndex{
  151. return _lastIndex;
  152. }
  153. @end