BDFaceAdjustParamsCell.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // BDFaceAdjustParamsCell.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by Zhang,Jian(MBD) on 2020/12/1.
  6. // Copyright © 2020 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceAdjustParamsCell.h"
  9. #import "BDFaceAdjustParamsItem.h"
  10. #import "BDFaceAdjustParamsConstants.h"
  11. #import "UIColor+BDFaceColorUtils.h"
  12. #import "BDFaceCalculateTool.h"
  13. float const BDFaceAdjustParamsCellHeight = 48.0f;
  14. static float const BDFaceAdjustParamsLeftButtonWidth = 50.0f;
  15. static float const BDFaceAdjustParamsLeftButtonHeight = 46.0f;
  16. static float const BDFaceAdjustParamsLeftButtonRightMargin = 10.0f;
  17. static float const BDFaceAdjustParamsNumberLabelWidth = 60.0f;
  18. static float const BDFaceAdjustParamsNumberLabelHeight = 46.0f;
  19. static float const BDFaceAdjustParamsNumberLabelRightMargin = BDFaceAdjustParamsLeftButtonRightMargin;
  20. static float const BDFaceAdjustParamsRightButtonWidth = BDFaceAdjustParamsLeftButtonWidth;
  21. static float const BDFaceAdjustParamsRightButtonHeight = BDFaceAdjustParamsLeftButtonHeight;
  22. static float const BDFaceAdjustParamsRightButtonRightMargin = 10.0f;
  23. @interface BDFaceAdjustParamsCell()
  24. @property(nonatomic, strong)UIButton *leftButton;
  25. @property(nonatomic, strong)UIButton *rightButton;
  26. @property(nonatomic, strong)UILabel *numberLabel;
  27. @end
  28. @implementation BDFaceAdjustParamsCell
  29. - (void)awakeFromNib {
  30. [super awakeFromNib];
  31. // Initialization code
  32. }
  33. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  34. [super setSelected:selected animated:animated];
  35. // Configure the view for the selected state
  36. }
  37. + (CGFloat)HeightOfFaceAdjustCell {
  38. return BDFaceAdjustParamsCellHeight;
  39. }
  40. - (void)cellFinishLoad:(NSInteger)rowsInSection {
  41. [super cellFinishLoad:rowsInSection];
  42. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  43. self.textLabel.text = item.itemTitle;
  44. self.textLabel.textColor = [UIColor blackColor];
  45. self.backgroundColor = [UIColor whiteColor];
  46. if (!self.leftButton) {
  47. self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
  48. [self addSubview:self.leftButton];
  49. }
  50. if (!self.numberLabel) {
  51. self.numberLabel = [[UILabel alloc]init];
  52. self.numberLabel.textAlignment = NSTextAlignmentCenter;
  53. [self addSubview:self.numberLabel];
  54. }
  55. self.numberLabel.text = [self numberString:item.currentValue];
  56. [self.numberLabel layoutIfNeeded];
  57. if (!self.rightButton) {
  58. self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  59. [self addSubview:self.rightButton];
  60. }
  61. self.selectionStyle = UITableViewCellSelectionStyleNone;
  62. [[NSNotificationCenter defaultCenter] removeObserver:self];
  63. if (item.contentType == BDFaceAdjustParamsContentTypeRecoverToNormal) {
  64. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(configisChange:) name:BDFaceAdjustParamsControllerConfigDidChangeNotification object:nil];
  65. }
  66. }
  67. - (void)dealloc {
  68. [[NSNotificationCenter defaultCenter] removeObserver:self];
  69. }
  70. - (void)configisChange:(NSNotification *)notification {
  71. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  72. if (item.contentType == BDFaceAdjustParamsContentTypeRecoverToNormal) {
  73. NSDictionary *dic = notification.object;
  74. if ([BDFaceCalculateTool noNullDic:dic]) {
  75. NSNumber *number = dic[BDFaceAdjustParamsControllerConfigIsSameKey];
  76. self.isSameConfig = number.boolValue;
  77. if (number.boolValue) {
  78. self.textLabel.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverUnactiveTextColor];
  79. } else {
  80. self.textLabel.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverActiveTextColor];
  81. }
  82. }
  83. } else {
  84. self.textLabel.textColor = [UIColor blackColor];
  85. }
  86. }
  87. - (void)layoutSubviews {
  88. [super layoutSubviews];
  89. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  90. CGFloat leftButtonOriginX = CGRectGetWidth(self.frame) - BDFaceAdjustParamsLeftButtonWidth - BDFaceAdjustParamsLeftButtonRightMargin - BDFaceAdjustParamsNumberLabelWidth - BDFaceAdjustParamsNumberLabelRightMargin - BDFaceAdjustParamsRightButtonWidth - BDFaceAdjustParamsRightButtonRightMargin;
  91. self.leftButton.frame = CGRectMake(leftButtonOriginX, (CGRectGetHeight(self.frame) - BDFaceAdjustParamsLeftButtonHeight) / 2.0f, BDFaceAdjustParamsLeftButtonWidth, BDFaceAdjustParamsLeftButtonHeight);
  92. self.numberLabel.frame = CGRectMake(CGRectGetMaxX(self.leftButton.frame) + BDFaceAdjustParamsLeftButtonRightMargin, (CGRectGetHeight(self.frame) - BDFaceAdjustParamsNumberLabelHeight) / 2.0f, BDFaceAdjustParamsNumberLabelWidth, BDFaceAdjustParamsNumberLabelHeight);
  93. self.rightButton.frame = CGRectMake(CGRectGetMaxX(self.numberLabel.frame) + BDFaceAdjustParamsNumberLabelRightMargin, (CGRectGetHeight(self.frame) - BDFaceAdjustParamsRightButtonHeight) / 2.0f, BDFaceAdjustParamsRightButtonWidth, BDFaceAdjustParamsRightButtonHeight);
  94. self.numberLabel.textColor = [UIColor blackColor];
  95. [self.leftButton addTarget:self action:@selector(minusNumber) forControlEvents:UIControlEventTouchUpInside];
  96. [self.rightButton addTarget:self action:@selector(addNumber) forControlEvents:UIControlEventTouchUpInside];
  97. if (self.indexPath.row == 0) {
  98. [self setCellsConerRadius:UIRectCornerTopLeft | UIRectCornerTopRight];
  99. }
  100. if ((self.indexPath.section == 0 && self.indexPath.row == 2)
  101. || (self.indexPath.section == 1 && self.indexPath.row == 6)
  102. || (self.indexPath.section == 2 && self.indexPath.row == 2)) {
  103. [self setCellsConerRadius:UIRectCornerBottomLeft | UIRectCornerBottomRight];
  104. }
  105. [self setConerRadius:BDFaceAdjustConfigTableCornerRadius borderWidth:(1.0f / UIScreen.mainScreen.scale) borderColor:nil];
  106. if (item.contentType == BDFaceAdjustParamsContentTypeRecoverToNormal) {
  107. self.rightButton.hidden = YES;
  108. self.leftButton.hidden = YES;
  109. self.numberLabel.hidden = YES;
  110. self.textLabel.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverUnactiveTextColor];
  111. self.textLabel.font = [UIFont boldSystemFontOfSize:16.0];
  112. } else {
  113. self.rightButton.hidden = NO;
  114. self.leftButton.hidden = NO;
  115. self.numberLabel.hidden = NO;
  116. self.textLabel.textColor = [UIColor blackColor];
  117. self.textLabel.font = [UIFont systemFontOfSize:16.0];
  118. }
  119. [self resetTitleViewColor];
  120. [self resetPlusAndMinusButtons];
  121. }
  122. - (void)resetPlusAndMinusButtons {
  123. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  124. NSString *currentValue = [self numberString:item.currentValue];
  125. NSString *maxValue = [self numberString:item.maxValue];
  126. NSString *minValue = [self numberString:item.minValue];
  127. if (currentValue.floatValue == minValue.floatValue) {
  128. [self.leftButton setImage:[UIImage imageNamed:@"left_button_unable"] forState:UIControlStateNormal];
  129. [self.leftButton setImage:[UIImage imageNamed:@"left_button_unable"] forState:UIControlStateHighlighted];
  130. } else {
  131. [self.leftButton setImage:[UIImage imageNamed:@"left_button_normal"] forState:UIControlStateNormal];
  132. [self.leftButton setImage:[UIImage imageNamed:@"left_button_highlight"] forState:UIControlStateHighlighted];
  133. }
  134. if (currentValue.floatValue == maxValue.floatValue) {
  135. [self.rightButton setImage:[UIImage imageNamed:@"right_button_unable"] forState:UIControlStateNormal];
  136. [self.rightButton setImage:[UIImage imageNamed:@"right_button_unable"] forState:UIControlStateHighlighted];
  137. } else {
  138. [self.rightButton setImage:[UIImage imageNamed:@"right_button_normal"] forState:UIControlStateNormal];
  139. [self.rightButton setImage:[UIImage imageNamed:@"right_button_highlight"] forState:UIControlStateHighlighted];
  140. }
  141. }
  142. - (void)resetTitleViewColor {
  143. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  144. if (item.contentType == BDFaceAdjustParamsContentTypeRecoverToNormal) {
  145. if (self.isSameConfig) {
  146. self.textLabel.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverUnactiveTextColor];
  147. } else {
  148. self.textLabel.textColor = [UIColor face_colorWithRGBHex:BDFaceAdjustParamsRecoverActiveTextColor];
  149. }
  150. } else {
  151. self.textLabel.textColor = [UIColor blackColor];
  152. }
  153. }
  154. - (void)setCellsConerRadius:(UIRectCorner)corner{
  155. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(15, 15)];
  156. CAShapeLayer *layer = [[CAShapeLayer alloc]init];
  157. layer.frame = self.bounds;
  158. layer.path = path.CGPath;
  159. self.layer.mask = layer;
  160. }
  161. - (void)minusNumber {
  162. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  163. if (item.currentValue > item.minValue) {
  164. item.currentValue = item.currentValue - item.interval;
  165. self.numberLabel.text = [self numberString:item.currentValue];
  166. if (self.didFinishAdjustParams) {
  167. self.didFinishAdjustParams(item.configDetailType, item.currentValue);
  168. }
  169. }
  170. [self resetPlusAndMinusButtons];
  171. }
  172. - (NSString *)numberString:(float)number {
  173. NSString *str = [NSString stringWithFormat:@"%0.2f", number];
  174. return [NSString stringWithFormat:@"%@", @(str.floatValue)];
  175. }
  176. - (void)addNumber {
  177. BDFaceAdjustParamsItem *item = (BDFaceAdjustParamsItem *)self.data;
  178. if (item.currentValue < item.maxValue) {
  179. item.currentValue = item.currentValue + item.interval;
  180. self.numberLabel.text = [self numberString:item.currentValue];
  181. if (self.didFinishAdjustParams) {
  182. self.didFinishAdjustParams(item.configDetailType, item.currentValue);
  183. }
  184. }
  185. [self resetPlusAndMinusButtons];
  186. }
  187. @end