MORankBottomButton.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // MORankBottomButton.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/9/10.
  6. //
  7. #import "MORankBottomButton.h"
  8. @interface MORankBottomButton ()
  9. @property (nonatomic, strong) UILabel *countLabel;
  10. @property (nonatomic, strong) UILabel *descLabel;
  11. @property (nonatomic, strong) UIView *coverView;//实现按下的背景色
  12. @end
  13. @implementation MORankBottomButton
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. if (self = [super initWithFrame:frame]) {
  16. self.layer.cornerRadius = 20;
  17. self.layer.masksToBounds = YES;
  18. _iconView = [[UIImageView alloc] init];
  19. _iconView.tintColor = UIColor.redColor;
  20. [self addSubview:_iconView];
  21. _countLabel = [[UILabel alloc] init];
  22. _countLabel.font = [MOTextTools mediumFont:16];
  23. _countLabel.textColor = UIColor.blackColor;
  24. [self addSubview:_countLabel];
  25. _descLabel = [[UILabel alloc] init];
  26. _descLabel.font = [MOTextTools regularFont:12];
  27. _descLabel.textColor = UIColor.grayColor;
  28. [self addSubview:_descLabel];
  29. [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.centerY.equalTo(_countLabel);
  31. make.right.equalTo(_countLabel.mas_left).offset(-4);
  32. make.width.height.mas_equalTo(20);
  33. }];
  34. [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.centerX.equalTo(self).offset(10);
  36. make.centerY.equalTo(self).offset(-8);
  37. }];
  38. [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.top.equalTo(_countLabel.mas_bottom).offset(2);
  40. make.centerX.equalTo(self);
  41. }];
  42. [self addSubview:self.coverView];
  43. [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.edges.mas_equalTo(0);
  45. }];
  46. }
  47. return self;
  48. }
  49. - (void)setHighlighted:(BOOL)highlighted {
  50. [super setHighlighted:highlighted];
  51. self.coverView.hidden = !highlighted;
  52. }
  53. - (void)configureWithCount:(NSString *)count
  54. desc:(NSString *)desc
  55. isGradient:(BOOL)isGradient {
  56. _countLabel.text = count;
  57. _descLabel.text = desc;
  58. if (isGradient) {
  59. [self layoutIfNeeded];
  60. // 移除之前的渐变层(避免重复添加)
  61. CALayer *oldLayer = [self.layer.sublayers firstObject];
  62. if ([oldLayer isKindOfClass:[CAGradientLayer class]]) {
  63. [oldLayer removeFromSuperlayer];
  64. }
  65. CAGradientLayer *gradient = [CAGradientLayer layer];
  66. gradient.frame = self.bounds;
  67. gradient.colors = @[(__bridge id)kBaseColorLeft.CGColor,
  68. (__bridge id)kBaseColorRight.CGColor];
  69. gradient.startPoint = CGPointMake(0, 0.5);
  70. gradient.endPoint = CGPointMake(1, 0.5);
  71. gradient.cornerRadius = self.layer.cornerRadius;
  72. [self.layer insertSublayer:gradient atIndex:0];
  73. _countLabel.textColor = UIColor.whiteColor;
  74. _descLabel.textColor = UIColor.whiteColor;
  75. } else {
  76. self.backgroundColor = UIColor.whiteColor;
  77. self.layer.borderColor = [MOTools colorWithHexString:@"#DADCE6"].CGColor;
  78. self.layer.borderWidth = 1;
  79. _countLabel.textColor = kBaseTextColor_1;
  80. _descLabel.textColor = kBaseTextColor_1;
  81. }
  82. }
  83. - (UIView *)coverView {
  84. if (!_coverView) {
  85. _coverView = [[UIView alloc] init];
  86. _coverView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.2];
  87. _coverView.hidden = YES;
  88. }
  89. return _coverView;
  90. }
  91. @end