// // HomeRankCardCell.m // keyBoard // // Created by Codex on 2025/11/06. // @import UIKit; #import "HomeRankCardCell.h" #import "UIView+KBShadow.h" #import "UIImageView+KBWebImage.h" @interface HomeRankCardCell () @property (nonatomic, strong) UIView *shadowView; // 阴影容器(不裁剪) @property (nonatomic, strong) UIView *cardView; // 卡片背景图片 @property (nonatomic, strong) UIImageView *averImageView; // 顶部圆形描边 @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *descLabel; @property (nonatomic, strong) UILabel *peopleLabel; @property (nonatomic, strong) UIButton *actionBtn; // 加号/勾选按钮 @property (nonatomic, assign) BOOL added; @end @implementation HomeRankCardCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.contentView.backgroundColor = UIColor.clearColor; [self setupUI]; [self setupConstraints]; } return self; } - (void)setupUI { // 使用懒加载属性,保证首次访问时创建 [self.contentView addSubview:self.shadowView]; [self.shadowView addSubview:self.cardView]; [self.contentView addSubview:self.averImageView]; [self.cardView addSubview:self.titleLabel]; [self.cardView addSubview:self.descLabel]; [self.cardView addSubview:self.peopleLabel]; [self.cardView addSubview:self.actionBtn]; // 统一阴影封装:在阴影容器上添加卡片阴影,阴影路径根据卡片左右 8pt 内边距进行收缩 [self.shadowView kb_setCardShadowWithCornerRadius:18.0 insets:UIEdgeInsetsMake(0, 8, 0, 8)]; } - (void)setupConstraints { // Masonry 约束 [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.contentView); make.top.equalTo(self.contentView.mas_top).offset(68 * 0.5); // 给圆环留空间 }]; [self.cardView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.shadowView).insets(UIEdgeInsetsMake(0, 8, 0, 8)); }]; [self.averImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.cardView); make.centerY.equalTo(self.shadowView.mas_top); make.width.height.mas_equalTo(68); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.averImageView.mas_bottom).offset(13); make.left.equalTo(self.cardView.mas_left).offset(18); make.right.equalTo(self.cardView.mas_right).offset(-18); make.height.mas_equalTo(23); }]; [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.titleLabel.mas_bottom).offset(12); make.left.equalTo(self.cardView.mas_left).offset(18); make.right.equalTo(self.cardView.mas_right).offset(-18); }]; [self.peopleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.descLabel.mas_bottom).offset(8); make.left.equalTo(self.cardView.mas_left).offset(18); make.right.equalTo(self.cardView.mas_right).offset(-18); }]; [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.cardView.mas_left).offset(18); make.right.equalTo(self.cardView.mas_right).offset(-18); make.bottom.equalTo(self.cardView.mas_bottom).offset(-12); make.height.mas_equalTo(32); }]; } - (void)layoutSubviews { [super layoutSubviews]; // 阴影路径需在尺寸确定后更新(封装方法会基于记录参数自动计算) [self.shadowView kb_updateShadowPath]; self.averImageView.layer.cornerRadius = 34; self.averImageView.layer.masksToBounds = true; } - (void)prepareForReuse { [super prepareForReuse]; self.onTapAction = nil; } - (void)tapAction { if (self.onTapAction) self.onTapAction(); } - (void)setCharacter:(KBCharacter *)character { _character = character; BOOL added = character.added; self.added = added; self.titleLabel.text = character.characterName; self.descLabel.text = character.characterBackground; self.peopleLabel.text = character.download; [self.averImageView kb_setAvatarURL:character.avatarUrl placeholder:KBPlaceholderImage]; if (added) { // 已添加:灰底、对勾 self.actionBtn.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1.0]; [self.actionBtn setTitle:@"✓" forState:UIControlStateNormal]; [self.actionBtn setTitleColor:[UIColor colorWithWhite:0.55 alpha:1.0] forState:UIControlStateNormal]; } else { // 未添加:黑底、加号 self.actionBtn.backgroundColor = [UIColor blackColor]; [self.actionBtn setTitle:@"+" forState:UIControlStateNormal]; [self.actionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; } // 已添加后禁用按钮(不可再次点击) self.actionBtn.enabled = !added; } #pragma mark - Lazy UI - (UIView *)shadowView { if (!_shadowView) { _shadowView = [UIView new]; _shadowView.backgroundColor = UIColor.clearColor; } return _shadowView; } - (UIView *)cardView { if (!_cardView) { _cardView = [[UIView alloc] init]; _cardView.backgroundColor = UIColor.whiteColor; } return _cardView; } - (UIImageView *)averImageView { if (!_averImageView) { _averImageView = [UIImageView new]; _averImageView.backgroundColor = UIColor.whiteColor; _averImageView.layer.borderColor = [UIColor colorWithRed:0.78 green:0.90 blue:0.20 alpha:1.0].CGColor; _averImageView.layer.borderWidth = 3.0; } return _averImageView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A]; _titleLabel.font = [KBFont medium:16]; _titleLabel.textAlignment = NSTextAlignmentCenter; } return _titleLabel; } - (UILabel *)descLabel { if (!_descLabel) { _descLabel = [UILabel new]; _descLabel.textColor = [UIColor colorWithHex:0x9A9A9A]; _descLabel.font = [KBFont regular:12]; _descLabel.textAlignment = NSTextAlignmentCenter; _descLabel.numberOfLines = 2; } return _descLabel; } - (UILabel *)peopleLabel { if (!_peopleLabel) { _peopleLabel = [UILabel new]; _peopleLabel.textColor = [UIColor colorWithHex:KBColorValue]; _peopleLabel.font = [KBFont regular:10]; _peopleLabel.textAlignment = NSTextAlignmentCenter; _peopleLabel.numberOfLines = 2; } return _peopleLabel; } - (UIButton *)actionBtn { if (!_actionBtn) { _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _actionBtn.layer.cornerRadius = 4.0; _actionBtn.layer.masksToBounds = YES; [_actionBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal]; _actionBtn.titleLabel.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold]; [_actionBtn addTarget:self action:@selector(tapAction) forControlEvents:UIControlEventTouchUpInside]; } return _actionBtn; } @end