FUBeautyShapeView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // FUBeautyShapeView.m
  3. // FUBeautyComponent
  4. //
  5. // Created by 项林平 on 2022/7/8.
  6. //
  7. #import "FUBeautyShapeView.h"
  8. #import "FUBeautyDefine.h"
  9. #import "FUCommonUIComponent.h"
  10. static NSString * const kFUBeautyShapeCellIdentifier = @"FUBeautyShapeCell";
  11. @interface FUBeautyShapeView ()<UICollectionViewDataSource, UICollectionViewDelegate>
  12. @property (nonatomic, strong) UICollectionView *shapeCollectionView;
  13. /// 程度调节
  14. @property (nonatomic, strong) FUSlider *slider;
  15. /// 恢复按钮
  16. @property (nonatomic, strong) FUSquareButton *recoverButton;
  17. @property (nonatomic, strong) FUBeautyShapeViewModel *viewModel;
  18. @end
  19. @implementation FUBeautyShapeView
  20. #pragma mark - Initializer
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. return [self initWithFrame:frame viewModel:[[FUBeautyShapeViewModel alloc] init]];
  23. }
  24. - (instancetype)initWithFrame:(CGRect)frame viewModel:(FUBeautyShapeViewModel *)viewModel {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. self.viewModel = viewModel;
  28. [self configureUI];
  29. [self refreshSubviews];
  30. }
  31. return self;
  32. }
  33. #pragma mark - UI
  34. - (void)configureUI {
  35. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  36. UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
  37. effectView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
  38. [self addSubview:effectView];
  39. [self addSubview:self.slider];
  40. [self addSubview:self.recoverButton];
  41. NSLayoutConstraint *recoverLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:17];
  42. NSLayoutConstraint *recoverBottomConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-6];
  43. NSLayoutConstraint *recoverWidthConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:44];
  44. NSLayoutConstraint *recoverHeightConstraint = [NSLayoutConstraint constraintWithItem:self.recoverButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:74];
  45. [self addConstraints:@[recoverLeadingConstraint, recoverBottomConstraint]];
  46. [self.recoverButton addConstraints:@[recoverWidthConstraint, recoverHeightConstraint]];
  47. // 分割线
  48. UIView *verticalLine = [[UIView alloc] init];
  49. verticalLine.backgroundColor = [UIColor colorWithRed:229/255.f green:229/255.f blue:229/255.f alpha:0.2];
  50. verticalLine.translatesAutoresizingMaskIntoConstraints = NO;
  51. [self addSubview:verticalLine];
  52. NSLayoutConstraint *lineLeadingConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.recoverButton attribute:NSLayoutAttributeTrailing multiplier:1 constant:14];
  53. NSLayoutConstraint *lineCenterYConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.recoverButton attribute:NSLayoutAttributeCenterY multiplier:1 constant:-15];
  54. NSLayoutConstraint *lineWidthConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:1];
  55. NSLayoutConstraint *lineHeightConstraint = [NSLayoutConstraint constraintWithItem:verticalLine attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:24];
  56. [self addConstraints:@[lineLeadingConstraint, lineCenterYConstraint]];
  57. [verticalLine addConstraints:@[lineWidthConstraint, lineHeightConstraint]];
  58. [self addSubview:self.shapeCollectionView];
  59. NSLayoutConstraint *collectionLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.shapeCollectionView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:76];
  60. NSLayoutConstraint *collectionTrailingConstraint = [NSLayoutConstraint constraintWithItem:self.shapeCollectionView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
  61. NSLayoutConstraint *collectionBottomConstraint = [NSLayoutConstraint constraintWithItem:self.shapeCollectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
  62. NSLayoutConstraint *collectionHeightConstraint = [NSLayoutConstraint constraintWithItem:self.shapeCollectionView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:98];
  63. [self addConstraints:@[collectionLeadingConstraint, collectionTrailingConstraint, collectionBottomConstraint]];
  64. [self.shapeCollectionView addConstraint:collectionHeightConstraint];
  65. }
  66. - (void)refreshSubviews {
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. if (self.viewModel.isDefaultValue) {
  69. self.recoverButton.alpha = 0.6;
  70. self.recoverButton.userInteractionEnabled = NO;
  71. } else {
  72. self.recoverButton.alpha = 1;
  73. self.recoverButton.userInteractionEnabled = YES;
  74. }
  75. if (!self.slider.hidden && self.viewModel.selectedIndex >= 0) {
  76. self.slider.bidirection = self.viewModel.beautyShapes[self.viewModel.selectedIndex].defaultValueInMiddle;
  77. self.slider.value = self.viewModel.beautyShapes[self.viewModel.selectedIndex].currentValue;
  78. }
  79. [self.shapeCollectionView reloadData];
  80. if (self.viewModel.selectedIndex >= 0) {
  81. [self.shapeCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.viewModel.selectedIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  82. }
  83. });
  84. }
  85. #pragma mark - Event response
  86. - (void)recoverAction {
  87. [FUAlertManager showAlertWithTitle:nil message:FUBeautyStringWithKey(@"是否将所有参数恢复到默认值") cancel:FUBeautyStringWithKey(@"取消") confirm:FUBeautyStringWithKey(@"确定") inController:nil confirmHandler:^{
  88. [self.viewModel recoverAllShapeValuesToDefault];
  89. [self refreshSubviews];
  90. } cancelHandler:nil];
  91. }
  92. - (void)sliderValueChanged {
  93. [self.viewModel setShapeValue:self.slider.value];
  94. }
  95. - (void)sliderChangeEnded {
  96. [self refreshSubviews];
  97. }
  98. #pragma mark - Collection view data source
  99. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  100. return self.viewModel.beautyShapes.count;
  101. }
  102. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  103. FUBeautyShapeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFUBeautyShapeCellIdentifier forIndexPath:indexPath];
  104. FUBeautyShapeModel *shape = self.viewModel.beautyShapes[indexPath.item];
  105. cell.textLabel.text = FUBeautyStringWithKey(shape.name);
  106. cell.imageName = shape.name;
  107. cell.defaultInMiddle = shape.defaultValueInMiddle;
  108. cell.defaultValue = shape.defaultValue;
  109. cell.currentValue = shape.currentValue;
  110. // 处理低性能手机禁用特效
  111. if (shape.differentiateDevicePerformance) {
  112. cell.disabled = self.viewModel.performanceLevel != FUDevicePerformanceLevelHigh;
  113. } else {
  114. cell.disabled = NO;
  115. }
  116. cell.selected = indexPath.item == self.viewModel.selectedIndex;
  117. return cell;
  118. }
  119. #pragma mark - Collection view delegate
  120. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  121. FUBeautyShapeModel *shape = self.viewModel.beautyShapes[indexPath.item];
  122. if (shape.differentiateDevicePerformance) {
  123. if (self.viewModel.performanceLevel != FUDevicePerformanceLevelHigh) {
  124. [FUTipHUD showTips:[NSString stringWithFormat:FUBeautyStringWithKey(@"该功能只支持在高端机上使用"), FUBeautyStringWithKey(shape.name)] dismissWithDelay:1];
  125. [self.shapeCollectionView reloadData];
  126. if (self.viewModel.selectedIndex >= 0) {
  127. [self.shapeCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.viewModel.selectedIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  128. }
  129. return NO;
  130. }
  131. }
  132. return YES;
  133. }
  134. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  135. if (indexPath.item == self.viewModel.selectedIndex) {
  136. return;
  137. }
  138. self.viewModel.selectedIndex = indexPath.item;
  139. FUBeautyShapeModel *shape = self.viewModel.beautyShapes[indexPath.item];
  140. if (self.slider.hidden) {
  141. self.slider.hidden = NO;
  142. }
  143. self.slider.bidirection = shape.defaultValueInMiddle;
  144. self.slider.value = shape.currentValue;
  145. }
  146. #pragma mark - Getters
  147. - (UICollectionView *)shapeCollectionView {
  148. if (!_shapeCollectionView) {
  149. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  150. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  151. layout.itemSize = CGSizeMake(44, 74);
  152. layout.minimumLineSpacing = 22;
  153. layout.minimumInteritemSpacing = 22;
  154. layout.sectionInset = UIEdgeInsetsMake(16, 16, 6, 16);
  155. _shapeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  156. _shapeCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
  157. _shapeCollectionView.backgroundColor = [UIColor clearColor];
  158. _shapeCollectionView.showsVerticalScrollIndicator = NO;
  159. _shapeCollectionView.showsHorizontalScrollIndicator = NO;
  160. _shapeCollectionView.dataSource = self;
  161. _shapeCollectionView.delegate = self;
  162. [_shapeCollectionView registerClass:[FUBeautyShapeCell class] forCellWithReuseIdentifier:kFUBeautyShapeCellIdentifier];
  163. }
  164. return _shapeCollectionView;
  165. }
  166. - (FUSquareButton *)recoverButton {
  167. if (!_recoverButton) {
  168. _recoverButton = [[FUSquareButton alloc] initWithFrame:CGRectMake(0, 0, 44, 74)];
  169. [_recoverButton setTitle:FUBeautyStringWithKey(@"恢复") forState:UIControlStateNormal];
  170. [_recoverButton setImage:[UIImage imageNamed:@"recover_item"] forState:UIControlStateNormal];
  171. _recoverButton.alpha = 0.6;
  172. _recoverButton.userInteractionEnabled = NO;
  173. [_recoverButton addTarget:self action:@selector(recoverAction) forControlEvents:UIControlEventTouchUpInside];
  174. _recoverButton.translatesAutoresizingMaskIntoConstraints = NO;
  175. }
  176. return _recoverButton;
  177. }
  178. -(FUSlider *)slider {
  179. if (!_slider) {
  180. _slider = [[FUSlider alloc] initWithFrame:CGRectMake(56, 16, CGRectGetWidth(self.frame) - 116, FUBeautyFunctionSliderHeight)];
  181. _slider.hidden = YES;
  182. [_slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
  183. [_slider addTarget:self action:@selector(sliderChangeEnded) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
  184. }
  185. return _slider;
  186. }
  187. @end
  188. @interface FUBeautyShapeCell ()
  189. @property (nonatomic, strong) UIImageView *imageView;
  190. @property (nonatomic, strong) UILabel *textLabel;
  191. @end
  192. @implementation FUBeautyShapeCell
  193. - (instancetype)initWithFrame:(CGRect)frame {
  194. self = [super initWithFrame:frame];
  195. if (self) {
  196. [self.contentView addSubview:self.imageView];
  197. NSLayoutConstraint *imageTop = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
  198. NSLayoutConstraint *imageLeading = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
  199. NSLayoutConstraint *imageTrailing = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
  200. NSLayoutConstraint *imageHeight = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
  201. [self.contentView addConstraints:@[imageTop, imageLeading, imageTrailing]];
  202. [self.imageView addConstraint:imageHeight];
  203. [self.contentView addSubview:self.textLabel];
  204. NSLayoutConstraint *textTop = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeBottom multiplier:1 constant:7];
  205. NSLayoutConstraint *textLeading = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
  206. NSLayoutConstraint *textTrailing = [NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
  207. [self.contentView addConstraints:@[textTop, textLeading, textTrailing]];
  208. }
  209. return self;
  210. }
  211. - (void)setSelected:(BOOL)selected {
  212. [super setSelected:selected];
  213. if (self.disabled) {
  214. self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-0", self.imageName]];
  215. self.imageView.alpha = 0.7;
  216. self.textLabel.alpha = 0.7;
  217. } else {
  218. self.imageView.alpha = 1;
  219. self.textLabel.alpha = 1;
  220. BOOL changed = NO;
  221. if (self.defaultInMiddle) {
  222. changed = fabs(self.currentValue - 0.5) > 0.01;
  223. }else{
  224. changed = self.currentValue > 0.01;
  225. }
  226. if (selected) {
  227. self.imageView.image = changed ? [UIImage imageNamed:[NSString stringWithFormat:@"%@-3", self.imageName]] : [UIImage imageNamed:[NSString stringWithFormat:@"%@-2", self.imageName]];
  228. self.textLabel.textColor = [UIColor colorWithRed:94/255.f green:199/255.f blue:254/255.f alpha:1];
  229. } else {
  230. self.imageView.image = changed ? [UIImage imageNamed:[NSString stringWithFormat:@"%@-1", self.imageName]] : [UIImage imageNamed:[NSString stringWithFormat:@"%@-0", self.imageName]];
  231. self.textLabel.textColor = [UIColor whiteColor];
  232. }
  233. }
  234. }
  235. - (UIImageView *)imageView {
  236. if (!_imageView) {
  237. _imageView = [[UIImageView alloc] init];
  238. _imageView.translatesAutoresizingMaskIntoConstraints = NO;
  239. }
  240. return _imageView;
  241. }
  242. - (UILabel *)textLabel {
  243. if (!_textLabel) {
  244. _textLabel = [[UILabel alloc] init];
  245. _textLabel.font = [UIFont systemFontOfSize:10];
  246. _textLabel.textColor = [UIColor whiteColor];
  247. _textLabel.textAlignment = NSTextAlignmentCenter;
  248. _textLabel.adjustsFontSizeToFitWidth = YES;
  249. _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
  250. }
  251. return _textLabel;
  252. }
  253. @end