| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- //MHBeautyView.m
- //美颜页面
- #import "MHBeautyView.h"
- #import "MHBeautyMenuCell.h"
- #import "MHBeautyParams.h"
- #import "MHBeautiesModel.h"
- @interface MHBeautyView ()<UICollectionViewDelegate,UICollectionViewDataSource>
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray *array;
- @property (nonatomic, assign) NSInteger lastIndex;
- @property (nonatomic, assign) NSInteger beautyType;
- @property (nonatomic, strong) NSMutableArray *arr;
- @end
- @implementation MHBeautyView
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- [self addSubview:self.collectionView];
- self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:MHBlackAlpha];
- NSDictionary *indexDic = [[NSUserDefaults standardUserDefaults] objectForKey:kMHBeautyTitle];
- if(IsDictionaryWithAnyKeyValue(indexDic)){
- NSNumber *index = indexDic.allValues.firstObject;
- if(index){
- self.lastIndex = index.integerValue;
- }else{
- self.lastIndex = -1;
- }
- }
- }
- return self;
- }
- - (void)configureBeautyData:(NSMutableArray *)beautyArr{
- self.array = beautyArr;
- [self.collectionView reloadData];
- }
- - (void)clearAllBeautyEffects {
- for (int i = 0; i<self.array.count; i++) {
- NSString *beautKey = [NSString stringWithFormat:@"beauty_%ld",(long)i];
- [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:beautKey];
- MHBeautiesModel *model = self.array[i];
- model.isSelected = i==0;
- }
- [self.collectionView reloadData];
- }
- - (void)cancelSelectedBeautyType:(NSInteger)type {
- for (int i = 0; i<self.array.count; i++) {
- MHBeautiesModel *model = self.array[i];
- if (model.type == type) {
- model.isSelected = NO;
- }
- }
- self.lastIndex = -1;
- [self.collectionView reloadData];
- }
- #pragma mark - collectionView
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.array.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHBeautyMenuCell" forIndexPath:indexPath];
- cell.beautyModel = self.array[indexPath.row];
- return cell;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView
- layout:(UICollectionViewLayout *)collectionViewLayout
- sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- return CGSizeMake((window_width-40)/5, MHMeiyanMenusCellHeight);
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- if (self.lastIndex == indexPath.row) {
- return;
- }
- MHBeautiesModel *currentModel = self.array[indexPath.row];
- currentModel.isSelected = YES;
- if (self.lastIndex >= 0) {
- MHBeautiesModel *lastModel = self.array[self.lastIndex];
- lastModel.isSelected = NO;
- }
- NSDictionary *indexDic = @{currentModel.beautyTitle:@(indexPath.row)};
- [[NSUserDefaults standardUserDefaults] setValue:indexDic forKey:kMHBeautyTitle];
- if (indexPath.row == 0) {
- [self clearAllBeautyEffects];
- }else{
- MHBeautiesModel *firstModel = self.array[0];
- firstModel.isSelected = NO;
- }
-
- self.lastIndex = indexPath.row;
- [self.collectionView reloadData];
-
- NSString *beautKey = [NSString stringWithFormat:@"beauty_%ld",(long)currentModel.type];
- NSInteger currentValue = [[NSUserDefaults standardUserDefaults] integerForKey:beautKey];
- if ([self.delegate respondsToSelector:@selector(handleBeautyEffects:sliderValue:name:)]) {
- [self.delegate handleBeautyEffects:currentModel.type sliderValue:currentValue name:currentModel.beautyTitle];
- }
- }
- #pragma mark - lazy
- //- (NSMutableArray *)array {
- // if (!_array) {
- // NSMutableArray * selectedItemArray = [MHSDK shareInstance].skinArray;
- // NSString *path = [[NSBundle mainBundle] pathForResource:@"MHBeautyParams" ofType:@"plist"];
- // NSArray *items = [NSArray arrayWithContentsOfFile:path];
- //
- // NSMutableArray * selectedItems = [NSMutableArray array];
- // for (int i = 0; i < selectedItemArray.count; i ++) {
- // NSDictionary * selectedItemDic = selectedItemArray[i];
- // NSString * selectedName = selectedItemDic[@"name"];
- // for (int j = 0; j < items.count; j++) {
- // NSDictionary * itemDic = items[j];
- // NSString * itemName = itemDic[@"name"];
- // if ([selectedName isEqual:itemName]) {
- // [selectedItems addObject:itemDic];
- // }
- // }
- // }
- // _array = [NSMutableArray array];
- // NSString *title = [[NSUserDefaults standardUserDefaults] valueForKey:kMHBeautyTitle];
- // for (int i = 0; i<selectedItems.count; i++) {
- // NSDictionary * itemDic = selectedItems[i];
- // MHBeautiesModel *model = [[MHBeautiesModel alloc] init];
- // model.imgName = itemDic[@"imageName"];
- // model.beautyTitle = itemDic[@"name"];
- // model.menuType = MHBeautyMenuType_Beauty;
- // model.type = [itemDic[@"type"] integerValue];
- // model.originalValue = itemDic[@"value"];
- // model.isSelected = [model.beautyTitle isEqualToString:title];
- //// NSString *beautKey = [NSString stringWithFormat:@"beauty_%ld",model.type];
- //// NSInteger originalValue = [[NSUserDefaults standardUserDefaults] integerForKey:beautKey];
- //// if (originalValue > 0) {
- //// model.originalValue = [NSString stringWithFormat:@"%ld",originalValue];
- //// [[NSUserDefaults standardUserDefaults] setInteger:originalValue forKey:beautKey];
- //// }
- // [_array addObject:model];
- // }
- // }
- // return _array;
- //}
- - (UICollectionView *)collectionView {
- if (!_collectionView) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.minimumLineSpacing = 0;
- layout.minimumInteritemSpacing = 0;
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- layout.sectionInset = UIEdgeInsetsMake(20, 20,20,20);
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width,self.frame.size.height) collectionViewLayout:layout];
- _collectionView.backgroundColor = [UIColor clearColor];
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHBeautyMenuCell"];
- }
- return _collectionView;
- }
- - (NSInteger)currentIndex{
- return _lastIndex;
- }
- @end
|