MHBeautyFaceView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // MHBeautyFaceView.m
  3. #import "MHBeautyFaceView.h"
  4. #import "MHBeautySlider.h"
  5. #import "MHBeautyMenuCell.h"
  6. #import "MHBeautyParams.h"
  7. #import "MHBeautiesModel.h"
  8. @interface MHBeautyFaceView ()<UICollectionViewDataSource,UICollectionViewDelegate>
  9. @property (nonatomic, strong) UICollectionView *collectionView;
  10. @property (nonatomic, strong) NSMutableArray *array;
  11. @property (nonatomic, assign) NSInteger lastIndex;
  12. @property (nonatomic, strong) MHBeautySlider *slider;
  13. @property (nonatomic, assign) NSInteger beautyType;
  14. @end
  15. @implementation MHBeautyFaceView
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. if (self = [super initWithFrame:frame]) {
  18. [self addSubview:self.collectionView];
  19. NSDictionary *indexDic = [[NSUserDefaults standardUserDefaults] objectForKey:kMHFaceTitle];
  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)configureFaceData:(NSMutableArray *)facesArr {
  32. self.array = facesArr;
  33. [self.collectionView reloadData];
  34. }
  35. - (void)clearAllFaceEffects {
  36. /****
  37. 瘦鼻 嘴型 下巴 额头 长鼻 眉毛 眼角 开眼角 眼距 ,默认值50,其余为0
  38. ***/
  39. NSArray *arr = @[@"瘦鼻", @"嘴型",@"下巴",@"额头",@"长鼻", @"眉毛", @"眼角", @"开眼角",@"眼距"];
  40. for (int i = 0; i<self.array.count; i++) {
  41. MHBeautiesModel *model = self.array[i];
  42. NSString *faceKey = [NSString stringWithFormat:@"face_%ld",model.type];
  43. if([arr containsObject:model.beautyTitle]){
  44. [[NSUserDefaults standardUserDefaults] setInteger:50 forKey:faceKey];
  45. }else{
  46. [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:faceKey];
  47. }
  48. model.isSelected = i==0;
  49. }
  50. [self.collectionView reloadData];
  51. }
  52. - (void)cancelSelectedFaceType:(NSInteger)type {
  53. for (int i = 0; i<self.array.count; i++) {
  54. MHBeautiesModel *model = self.array[i];
  55. if (model.type == type) {
  56. model.isSelected = NO;
  57. }
  58. }
  59. self.lastIndex = -1;
  60. [self.collectionView reloadData];
  61. }
  62. #pragma mark - collectionView
  63. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  64. return self.array.count;
  65. }
  66. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  67. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHBeautyMenuCell" forIndexPath:indexPath];
  68. cell.beautyModel = self.array[indexPath.row];
  69. return cell;
  70. }
  71. - (CGSize)collectionView:(UICollectionView *)collectionView
  72. layout:(UICollectionViewLayout *)collectionViewLayout
  73. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  74. return CGSizeMake((window_width - 20 - 5*20)/4, MHMeiyanMenusCellHeight);
  75. }
  76. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  77. if (self.lastIndex == indexPath.row) {
  78. return;
  79. }
  80. MHBeautiesModel *currentModel = self.array[indexPath.row];
  81. currentModel.isSelected = YES;
  82. if(self.lastIndex > 0){
  83. MHBeautiesModel *lastModel = self.array[self.lastIndex];
  84. lastModel.isSelected = NO;
  85. }
  86. if (indexPath.row == 0) {
  87. [self clearAllFaceEffects];
  88. }else{
  89. MHBeautiesModel *firstModel = self.array[0];
  90. firstModel.isSelected = NO;
  91. // NSString *key = [NSString stringWithFormat:@"kFace_%@",firstModel.beautyTitle];
  92. // [[NSUserDefaults standardUserDefaults] setBool:NO forKey:key];
  93. }
  94. self.lastIndex = indexPath.row;
  95. [self.collectionView reloadData];
  96. NSDictionary *indexDic = @{currentModel.beautyTitle:@(indexPath.row)};
  97. [[NSUserDefaults standardUserDefaults] setValue:indexDic forKey:kMHFaceTitle];
  98. NSString *faceKey = [NSString stringWithFormat:@"face_%ld",(long)currentModel.type];
  99. NSInteger currentValue = [[NSUserDefaults standardUserDefaults] integerForKey:faceKey];
  100. if ([self.delegate respondsToSelector:@selector(handleFaceEffects:sliderValue:name:)]) {
  101. [self.delegate handleFaceEffects:currentModel.type sliderValue:currentValue name:currentModel.beautyTitle];
  102. }
  103. }
  104. #pragma mark - lazy
  105. - (UICollectionView *)collectionView {
  106. if (!_collectionView) {
  107. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  108. layout.minimumLineSpacing = 0;
  109. layout.minimumInteritemSpacing = 20;
  110. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  111. layout.sectionInset = UIEdgeInsetsMake(20, 20,20,20);
  112. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width, self.frame.size.height) collectionViewLayout:layout];
  113. _collectionView.showsHorizontalScrollIndicator = NO;
  114. _collectionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:MHBlackAlpha];
  115. _collectionView.delegate = self;
  116. _collectionView.dataSource = self;
  117. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHBeautyMenuCell"];
  118. }
  119. return _collectionView;
  120. }
  121. - (NSMutableArray *)array {
  122. if (!_array) {
  123. _array = [NSMutableArray array];
  124. }
  125. return _array;
  126. }
  127. - (NSInteger)currentIndex
  128. {
  129. return _lastIndex;
  130. }
  131. @end