FUBeautyComponentManager.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // FUBeautyComponentManager.m
  3. // FUBeautyComponentManager
  4. //
  5. // Created by 项林平 on 2022/6/10.
  6. //
  7. #import "FUBeautyComponentManager.h"
  8. static FUBeautyComponentManager *beautyComponentManager = nil;
  9. static dispatch_once_t onceToken;
  10. @interface FUBeautyComponentManager ()<FUSegmentBarDelegate, FUBeautyFilterViewDelegate>
  11. @property (nonatomic, weak) UIView *targetView;
  12. @property (nonatomic, strong) FUSegmentBar *categoryView;
  13. @property (nonatomic, assign) NSInteger selectedIndex;
  14. @end
  15. @implementation FUBeautyComponentManager
  16. #pragma mark - Class methods
  17. + (instancetype)sharedManager {
  18. dispatch_once(&onceToken, ^{
  19. beautyComponentManager = [[FUBeautyComponentManager alloc] init];
  20. });
  21. return beautyComponentManager;
  22. }
  23. + (void)destory {
  24. onceToken = 0;
  25. beautyComponentManager = nil;
  26. }
  27. #pragma mark - Initializer
  28. - (instancetype)init {
  29. self = [super init];
  30. if (self) {
  31. self.selectedIndex = FUBeautyCategoryNone;
  32. }
  33. return self;
  34. }
  35. #pragma mark - Instance methods
  36. - (void)addComponentViewToView:(UIView *)view {
  37. NSAssert(view != nil, @"FUBeautyComponent: view can not be nil!");
  38. self.targetView = view;
  39. // [self removeComponentView];
  40. // [self.targetView addSubview:self.beautySkinView];
  41. // [self.targetView addSubview:self.beautyShapeView];
  42. // [self.targetView addSubview:self.beautyFilterView];
  43. // [self.targetView addSubview:self.categoryView];
  44. }
  45. - (void)loadBeauty {
  46. if (![FURenderKit shareRenderKit].beauty) {
  47. FUBeauty *beauty = [self defaultBeauty];
  48. [FURenderKit shareRenderKit].beauty = beauty;
  49. }
  50. // 分别设置美肤、美型、滤镜
  51. // 主线程执行:
  52. dispatch_async(dispatch_get_main_queue(), ^{
  53. [self.beautySkinViewModel setAllSkinValues];
  54. [self.beautyShapeViewModel setAllShapeValues];
  55. [self.beautyFilterViewModel setCurrentFilter];
  56. });
  57. }
  58. - (void)unloadBeauty {
  59. [FURenderKit shareRenderKit].beauty = nil;
  60. }
  61. - (void)saveBeauty {
  62. [self.beautySkinViewModel saveSkinsPersistently];
  63. [self.beautyShapeViewModel saveShapesPersistently];
  64. [self.beautyFilterViewModel saveFitersPersistently];
  65. }
  66. #pragma mark - Private methods
  67. /// 默认美颜
  68. - (FUBeauty *)defaultBeauty {
  69. NSString *path = [[NSBundle mainBundle] pathForResource:@"face_beautification" ofType:@"bundle"];
  70. FUBeauty *beauty = [[FUBeauty alloc] initWithPath:path name:@"FUBeauty"];
  71. beauty.heavyBlur = 0;
  72. // 默认均匀磨皮
  73. beauty.blurType = 3;
  74. // 默认精细变形
  75. beauty.faceShape = 4;
  76. // 高性能设备设置去黑眼圈、去法令纹、大眼、嘴型最新效果
  77. if ([FURenderKit devicePerformanceLevel] == FUDevicePerformanceLevelHigh) {
  78. [beauty addPropertyMode:FUBeautyPropertyMode2 forKey:FUModeKeyRemovePouchStrength];
  79. [beauty addPropertyMode:FUBeautyPropertyMode2 forKey:FUModeKeyRemoveNasolabialFoldsStrength];
  80. [beauty addPropertyMode:FUBeautyPropertyMode3 forKey:FUModeKeyEyeEnlarging];
  81. [beauty addPropertyMode:FUBeautyPropertyMode3 forKey:FUModeKeyIntensityMouth];
  82. }
  83. return beauty;
  84. }
  85. #pragma mark - Event response
  86. - (void)compareTouchDownAction {
  87. if (self.delegate && [self.delegate respondsToSelector:@selector(beautyComponentDidTouchDownComparison)]) {
  88. [self.delegate beautyComponentDidTouchDownComparison];
  89. }
  90. }
  91. - (void)compareTouchUpAction {
  92. if (self.delegate && [self.delegate respondsToSelector:@selector(beautyComponentDidTouchUpComparison)]) {
  93. [self.delegate beautyComponentDidTouchUpComparison];
  94. }
  95. }
  96. #pragma mark - FUSegmentBarDelegate
  97. - (void)segmentBar:(FUSegmentBar *)segmentBar didSelectItemAtIndex:(NSUInteger)index {
  98. if (index == self.selectedIndex) {
  99. // 隐藏
  100. // segmentBar.userInteractionEnabled = NO;
  101. // [self hideEffectView:[self showingView] animated:YES completion:^{
  102. // segmentBar.userInteractionEnabled = YES;
  103. // }];
  104. // self.compareButton.transform = CGAffineTransformIdentity;
  105. // self.compareButton.hidden = YES;
  106. // [segmentBar selectItemAtIndex:-1];
  107. // self.selectedIndex = FUBeautyCategoryNone;
  108. // if (self.delegate && [self.delegate respondsToSelector:@selector(beautyComponentViewHeightDidChange:)]) {
  109. // [self.delegate beautyComponentViewHeightDidChange:CGRectGetHeight(self.categoryView.frame)];
  110. // }
  111. // } else {
  112. // segmentBar.userInteractionEnabled = NO;
  113. // if (self.selectedIndex > FUBeautyCategoryNone) {
  114. // [self hideEffectView:[self showingView] animated:NO completion:nil];
  115. // }
  116. // self.selectedIndex = index;
  117. // [self showEffectView:[self showingView] animated:YES completion:^{
  118. // segmentBar.userInteractionEnabled = YES;
  119. // }];
  120. }
  121. }
  122. #pragma mark - FUBeautyFilterViewDelegate
  123. - (void)beautyFilterViewDidChangeFilter:(NSString *)name {
  124. if (self.delegate && [self.delegate respondsToSelector:@selector(beautyComponentNeedsDisplayPromptContent:)]) {
  125. [self.delegate beautyComponentNeedsDisplayPromptContent:name];
  126. }
  127. }
  128. #pragma mark - FUBeautyStyleViewDelegate
  129. - (void)beautyStyleViewDidSelectStyle {
  130. [self.categoryView refresh];
  131. }
  132. - (void)beautyStyleViewDidCancelStyle {
  133. [self.categoryView refresh];
  134. // 取消风格推荐,恢复美肤、美型、滤镜效果
  135. [self.beautySkinViewModel setAllSkinValues];
  136. [self.beautyShapeViewModel setAllShapeValues];
  137. [self.beautyFilterViewModel setCurrentFilter];
  138. }
  139. #pragma mark - Getters
  140. - (FUSegmentBar *)categoryView {
  141. if (!_categoryView) {
  142. NSArray<NSString *> *categories = @[@"美肤", @"美型", @"滤镜"];
  143. FUSegmentBarConfigurations *configurations = [[FUSegmentBarConfigurations alloc] init];
  144. configurations.titleFont = [UIFont systemFontOfSize:15];
  145. _categoryView = [[FUSegmentBar alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.targetView.bounds) - FUBeautyHeightIncludeBottomSafeArea(FUBeautyCategoryViewHeight), CGRectGetWidth(self.targetView.bounds), FUBeautyHeightIncludeBottomSafeArea(FUBeautyCategoryViewHeight)) titles:categories configuration:configurations];
  146. _categoryView.delegate = self;
  147. }
  148. return _categoryView;
  149. }
  150. //- (FUBeautySkinView *)beautySkinView {
  151. // if (!_beautySkinView) {
  152. // _beautySkinView = [[FUBeautySkinView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(self.categoryView.frame), CGRectGetWidth(self.targetView.bounds), FUBeautyFunctionViewOverallHeight) viewModel:self.beautySkinViewModel];
  153. // _beautySkinView.hidden = YES;
  154. // }
  155. // return _beautySkinView;
  156. //}
  157. //
  158. //- (FUBeautyShapeView *)beautyShapeView {
  159. // if (!_beautyShapeView) {
  160. // _beautyShapeView = [[FUBeautyShapeView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(self.categoryView.frame), CGRectGetWidth(self.targetView.bounds), FUBeautyFunctionViewOverallHeight) viewModel:self.beautyShapeViewModel];
  161. // _beautyShapeView.hidden = YES;
  162. // }
  163. // return _beautyShapeView;
  164. //}
  165. //- (FUBeautyFilterView *)beautyFilterView {
  166. // if (!_beautyFilterView) {
  167. // _beautyFilterView = [[FUBeautyFilterView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(self.categoryView.frame), CGRectGetWidth(self.targetView.bounds), FUBeautyFunctionViewOverallHeight) viewModel:self.beautyFilterViewModel];
  168. // _beautyFilterView.hidden = YES;
  169. // _beautyFilterView.delegate = self;
  170. // }
  171. // return _beautyFilterView;
  172. //}
  173. //FUBeautyComponentManager
  174. - (FUBeautySkinViewModel *)beautySkinViewModel {
  175. if (!_beautySkinViewModel) {
  176. _beautySkinViewModel = [[FUBeautySkinViewModel alloc] init];
  177. }
  178. return _beautySkinViewModel;
  179. }
  180. - (FUBeautyShapeViewModel *)beautyShapeViewModel {
  181. if (!_beautyShapeViewModel) {
  182. _beautyShapeViewModel = [[FUBeautyShapeViewModel alloc] init];
  183. }
  184. return _beautyShapeViewModel;
  185. }
  186. - (FUBeautyFilterViewModel *)beautyFilterViewModel {
  187. if (!_beautyFilterViewModel) {
  188. _beautyFilterViewModel = [[FUBeautyFilterViewModel alloc] init];
  189. }
  190. return _beautyFilterViewModel;
  191. }
  192. - (CGFloat)componentViewHeight {
  193. if (self.selectedIndex == FUBeautyCategoryNone) {
  194. return FUBeautyHeightIncludeBottomSafeArea(FUBeautyCategoryViewHeight);
  195. } else {
  196. return FUBeautyHeightIncludeBottomSafeArea(FUBeautyCategoryViewHeight) + FUBeautyFunctionViewOverallHeight;
  197. }
  198. }
  199. @end