123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- //
- // FUBeautySkinView.m
- // FUBeautyComponent
- //
- // Created by 项林平 on 2022/6/21.
- //
- #import "FUBeautySkinView.h"
- #import "FUBeautyDefine.h"
- #import "FUCommonUIComponent.h"
- static NSString * const kFUBeautySkinCellIdentifier = @"FUBeautySkinCell";
- @interface FUBeautySkinView ()<UICollectionViewDataSource, UICollectionViewDelegate>
- @property (nonatomic, strong) UICollectionView *skinCollectionView;
- /// 程度调节
- @property (nonatomic, strong) FUSlider *slider;
- /// 恢复按钮
- @property (nonatomic, strong) FUSquareButton *recoverButton;
- @property (nonatomic, strong) FUBeautySkinViewModel *viewModel;
- @end
- @implementation FUBeautySkinView
- #pragma mark - Initializer
- - (instancetype)initWithFrame:(CGRect)frame {
- return [self initWithFrame:frame viewModel:[[FUBeautySkinViewModel alloc] init]];
- }
- - (instancetype)initWithFrame:(CGRect)frame viewModel:(FUBeautySkinViewModel *)viewModel {
- self = [super initWithFrame:frame];
- if (self) {
- self.viewModel = viewModel;
- [self configureUI];
- [self refreshSubviews];
- }
- return self;
- }
- #pragma mark - UI
- - (void)configureUI {
- UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
- UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
- effectView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
- [self addSubview:effectView];
-
- [self addSubview:self.slider];
- [self addSubview:self.recoverButton];
- NSLayoutConstraint *recoverLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:17];
- NSLayoutConstraint *recoverBottomConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-6];
- NSLayoutConstraint *recoverWidthConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:44];
- NSLayoutConstraint *recoverHeightConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:74];
-
- [self addConstraints:@[recoverLeadingConstraint, recoverBottomConstraint]];
- [self.recoverButton addConstraints:@[recoverWidthConstraint, recoverHeightConstraint]];
-
- // 分割线
- UIView *verticalLine = [[UIView alloc] init];
- verticalLine.backgroundColor = [UIColor colorWithRed:229/255.f green:229/255.f blue:229/255.f alpha:0.2];
- verticalLine.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:verticalLine];
- NSLayoutConstraint *lineLeadingConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.recoverButton attribute:NSLayoutAttributeTrailing multiplier:1 constant:14];
- NSLayoutConstraint *lineCenterYConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.recoverButton attribute:NSLayoutAttributeCenterY multiplier:1 constant:-15];
- NSLayoutConstraint *lineWidthConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:1];
- NSLayoutConstraint *lineHeightConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:24];
- [self addConstraints:@[lineLeadingConstraint, lineCenterYConstraint]];
- [verticalLine addConstraints:@[lineWidthConstraint, lineHeightConstraint]];
-
- [self addSubview:self.skinCollectionView];
- NSLayoutConstraint *collectionLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.skinCollectionView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:76];
- NSLayoutConstraint *collectionTrailingConstraint = [NSLayoutConstraint constraintWithItem:self.skinCollectionView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
- NSLayoutConstraint *collectionBottomConstraint = [NSLayoutConstraint constraintWithItem:self.skinCollectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
- NSLayoutConstraint *collectionHeightConstraint = [NSLayoutConstraint constraintWithItem:self.skinCollectionView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:98];
- [self addConstraints:@[collectionLeadingConstraint, collectionTrailingConstraint, collectionBottomConstraint]];
- [self.skinCollectionView addConstraint:collectionHeightConstraint];
- }
- - (void)refreshSubviews {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.viewModel.isDefaultValue) {
- self.recoverButton.alpha = 0.6;
- self.recoverButton.userInteractionEnabled = NO;
- } else {
- self.recoverButton.alpha = 1;
- self.recoverButton.userInteractionEnabled = YES;
- }
- if (!self.slider.hidden && self.viewModel.selectedIndex >= 0) {
- self.slider.bidirection = self.viewModel.beautySkins[self.viewModel.selectedIndex].defaultValueInMiddle;
- self.slider.value = self.viewModel.beautySkins[self.viewModel.selectedIndex].currentValue / self.viewModel.beautySkins[self.viewModel.selectedIndex].ratio;
- }
- [self.skinCollectionView reloadData];
- if (self.viewModel.selectedIndex >= 0) {
- [self.skinCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.viewModel.selectedIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
- }
- });
- }
- #pragma mark - Event response
- - (void)recoverAction {
- [FUAlertManager showAlertWithTitle:nil message:FUBeautyStringWithKey(@"是否将所有参数恢复到默认值") cancel:FUBeautyStringWithKey(@"取消") confirm:FUBeautyStringWithKey(@"确定") inController:nil confirmHandler:^{
- [self.viewModel recoverAllSkinValuesToDefault];
- [self refreshSubviews];
- } cancelHandler:nil];
- }
- - (void)sliderValueChanged {
- [self.viewModel setSkinValue:self.slider.value];
- }
- - (void)sliderChangeEnded {
- [self refreshSubviews];
- }
- #pragma mark - Collection view data source
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.viewModel.beautySkins.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- FUBeautySkinCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFUBeautySkinCellIdentifier forIndexPath:indexPath];
- FUBeautySkinModel *skin = self.viewModel.beautySkins[indexPath.item];
- cell.textLabel.text = FUBeautyStringWithKey(skin.name);
- cell.imageName = skin.name;
- cell.defaultInMiddle = skin.defaultValueInMiddle;
- cell.defaultValue = skin.defaultValue;
- cell.currentValue = skin.currentValue;
- cell.selected = indexPath.item == self.viewModel.selectedIndex;
- return cell;
- }
- #pragma mark - Collection view delegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.item == self.viewModel.selectedIndex) {
- return;
- }
- self.viewModel.selectedIndex = indexPath.item;
- FUBeautySkinModel *skin = self.viewModel.beautySkins[indexPath.item];
- if (self.slider.hidden) {
- self.slider.hidden = NO;
- }
- self.slider.bidirection = skin.defaultValueInMiddle;
- self.slider.value = skin.currentValue / skin.ratio;
- }
- #pragma mark - Getters
- - (UICollectionView *)skinCollectionView {
- if (!_skinCollectionView) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- layout.itemSize = CGSizeMake(44, 74);
- layout.minimumLineSpacing = 22;
- layout.minimumInteritemSpacing = 22;
- layout.sectionInset = UIEdgeInsetsMake(16, 16, 6, 16);
- _skinCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- _skinCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
- _skinCollectionView.backgroundColor = [UIColor clearColor];
- _skinCollectionView.showsVerticalScrollIndicator = NO;
- _skinCollectionView.showsHorizontalScrollIndicator = NO;
- _skinCollectionView.dataSource = self;
- _skinCollectionView.delegate = self;
- [_skinCollectionView registerClass:[FUBeautySkinCell class] forCellWithReuseIdentifier:kFUBeautySkinCellIdentifier];
- }
- return _skinCollectionView;
- }
- - (FUSquareButton *)recoverButton {
- if (!_recoverButton) {
- _recoverButton = [[FUSquareButton alloc] initWithFrame:CGRectMake(0, 0, 44, 74)];
- [_recoverButton setTitle:FUBeautyStringWithKey(@"恢复") forState:UIControlStateNormal];
- [_recoverButton setImage:[UIImage imageNamed:@"recover_item"] forState:UIControlStateNormal];
- _recoverButton.alpha = 0.6;
- _recoverButton.userInteractionEnabled = NO;
- [_recoverButton addTarget:self action:@selector(recoverAction) forControlEvents:UIControlEventTouchUpInside];
- _recoverButton.translatesAutoresizingMaskIntoConstraints = NO;
- }
- return _recoverButton;
- }
- -(FUSlider *)slider {
- if (!_slider) {
- _slider = [[FUSlider alloc] initWithFrame:CGRectMake(56, 16, CGRectGetWidth(self.frame) - 116, FUBeautyFunctionSliderHeight)];
- _slider.hidden = YES;
- [_slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
- [_slider addTarget:self action:@selector(sliderChangeEnded) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
- }
- return _slider;
- }
- @end
- @interface FUBeautySkinCell ()
- @property (nonatomic, strong) UIImageView *imageView;
- @property (nonatomic, strong) UILabel *textLabel;
- @end
- @implementation FUBeautySkinCell
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self.contentView addSubview:self.imageView];
- NSLayoutConstraint *imageTop = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
- NSLayoutConstraint *imageLeading = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
- NSLayoutConstraint *imageTrailing = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
- NSLayoutConstraint *imageHeight = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
- [self.contentView addConstraints:@[imageTop, imageLeading, imageTrailing]];
- [self.imageView addConstraint:imageHeight];
-
- [self.contentView addSubview:self.textLabel];
- NSLayoutConstraint *textTop = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeBottom multiplier:1 constant:7];
- NSLayoutConstraint *textLeading = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
- NSLayoutConstraint *textTrailing = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
- [self.contentView addConstraints:@[textTop, textLeading, textTrailing]];
- }
- return self;
- }
- - (void)setSelected:(BOOL)selected {
- [super setSelected:selected];
- BOOL changed = NO;
- if (self.defaultInMiddle) {
- changed = fabs(self.currentValue - 0.5) > 0.01;
- }else{
- changed = self.currentValue > 0.01;
- }
- if (selected) {
- self.imageView.image = changed ? [UIImage imageNamed:[NSString stringWithFormat:@"%@-3", self.imageName]] : [UIImage imageNamed:[NSString stringWithFormat:@"%@-2", self.imageName]];
- self.textLabel.textColor = [UIColor colorWithRed:94/255.f green:199/255.f blue:254/255.f alpha:1];
- } else {
- self.imageView.image = changed ? [UIImage imageNamed:[NSString stringWithFormat:@"%@-1", self.imageName]] : [UIImage imageNamed:[NSString stringWithFormat:@"%@-0", self.imageName]];
- self.textLabel.textColor = [UIColor whiteColor];
- }
- }
- - (UIImageView *)imageView {
- if (!_imageView) {
- _imageView = [[UIImageView alloc] init];
- _imageView.translatesAutoresizingMaskIntoConstraints = NO;
- }
- return _imageView;
- }
- - (UILabel *)textLabel {
- if (!_textLabel) {
- _textLabel = [[UILabel alloc] init];
- _textLabel.font = [UIFont systemFontOfSize:10];
- _textLabel.textColor = [UIColor whiteColor];
- _textLabel.textAlignment = NSTextAlignmentCenter;
- _textLabel.adjustsFontSizeToFitWidth = YES;
- _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
- }
- return _textLabel;
- }
- @end
|