1
This commit is contained in:
@@ -15,6 +15,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
/// 当前展示的排行榜角色模型
|
||||
@property (nonatomic, strong, nullable) KBCharacter *character;
|
||||
|
||||
/// 点击右侧加号/勾选按钮的回调,由外部 VC 处理网络逻辑
|
||||
@property (nonatomic, copy, nullable) void (^onTapAction)(void);
|
||||
|
||||
/// 旧的配置方法(不再推荐使用),内部会转成 character 赋值
|
||||
- (void)configWithRank:(NSInteger)rank
|
||||
title:(NSString *)title
|
||||
|
||||
@@ -70,6 +70,12 @@
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)actionButtonTapped {
|
||||
if (self.onTapAction) {
|
||||
self.onTapAction();
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setCharacter:(KBCharacter *)character {
|
||||
_character = character;
|
||||
|
||||
@@ -94,6 +100,8 @@
|
||||
[self.actionButton setTitleColor:[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] forState:UIControlStateNormal];
|
||||
self.actionButton.backgroundColor = [UIColor colorWithRed:0.88 green:0.97 blue:0.93 alpha:1.0];
|
||||
}
|
||||
// 已加入后禁用按钮,避免重复添加或取消
|
||||
self.actionButton.enabled = !joined;
|
||||
}
|
||||
|
||||
// 兼容旧调用:转成一个临时模型再走统一逻辑
|
||||
@@ -111,6 +119,7 @@
|
||||
[self.avatarView kb_cancelImageLoad];
|
||||
self.avatarView.image = nil;
|
||||
self.character = nil;
|
||||
self.onTapAction = nil;
|
||||
}
|
||||
|
||||
#pragma mark - Lazy
|
||||
@@ -170,6 +179,7 @@
|
||||
_actionButton.layer.cornerRadius = 12;
|
||||
_actionButton.layer.masksToBounds = YES;
|
||||
_actionButton.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
|
||||
[_actionButton addTarget:self action:@selector(actionButtonTapped) forControlEvents:UIControlEventTouchUpInside];
|
||||
}
|
||||
return _actionButton;
|
||||
}
|
||||
|
||||
@@ -127,6 +127,8 @@
|
||||
[self.actionBtn setTitle:@"+" forState:UIControlStateNormal];
|
||||
[self.actionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
||||
}
|
||||
// 已添加后禁用按钮(不可再次点击)
|
||||
self.actionBtn.enabled = !added;
|
||||
}
|
||||
|
||||
#pragma mark - Lazy UI
|
||||
|
||||
@@ -16,6 +16,13 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
/// 使用前三名角色模型进行配置(数组元素为 KBCharacter*,最多取前三个)
|
||||
- (void)configWithCharacters:(NSArray<KBCharacter *> *)characters;
|
||||
|
||||
/// 中间卡片底部加号按钮点击回调(传入对应角色模型,可能为 nil)
|
||||
@property (nonatomic, copy, nullable) void (^onCenterPlusTapped)(KBCharacter * _Nullable character);
|
||||
/// 左侧卡片底部加号按钮点击回调
|
||||
@property (nonatomic, copy, nullable) void (^onLeftPlusTapped)(KBCharacter * _Nullable character);
|
||||
/// 右侧卡片底部加号按钮点击回调
|
||||
@property (nonatomic, copy, nullable) void (^onRightPlusTapped)(KBCharacter * _Nullable character);
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -135,6 +135,10 @@
|
||||
/// 左右卡片底部也有 plus 按钮
|
||||
@property (nonatomic, strong) UIButton *leftPlusButton;
|
||||
@property (nonatomic, strong) UIButton *rightPlusButton;
|
||||
/// 对应的前三名角色模型(用于回调和状态控制)
|
||||
@property (nonatomic, strong, nullable) KBCharacter *firstCharacter;
|
||||
@property (nonatomic, strong, nullable) KBCharacter *secondCharacter;
|
||||
@property (nonatomic, strong, nullable) KBCharacter *thirdCharacter;
|
||||
@end
|
||||
|
||||
@implementation KBTopThreeView
|
||||
@@ -203,21 +207,78 @@
|
||||
}
|
||||
|
||||
- (void)configWithCharacters:(NSArray<KBCharacter *> *)characters {
|
||||
if (characters.count == 0) {
|
||||
// 无数据时清空标题与头像
|
||||
self.firstCharacter = characters.count > 0 ? characters[0] : nil;
|
||||
self.secondCharacter = characters.count > 1 ? characters[1] : nil;
|
||||
self.thirdCharacter = characters.count > 2 ? characters[2] : nil;
|
||||
|
||||
if (self.firstCharacter) {
|
||||
[self.centerCard renderWithCharacter:self.firstCharacter rank:1];
|
||||
} else {
|
||||
[self.centerCard renderWithCharacter:[KBCharacter new] rank:1];
|
||||
[self.leftCard renderWithCharacter:[KBCharacter new] rank:2];
|
||||
[self.rightCard renderWithCharacter:[KBCharacter new] rank:3];
|
||||
return;
|
||||
}
|
||||
|
||||
KBCharacter *first = characters.count > 0 ? characters[0] : nil;
|
||||
KBCharacter *second = characters.count > 1 ? characters[1] : nil;
|
||||
KBCharacter *third = characters.count > 2 ? characters[2] : nil;
|
||||
if (self.secondCharacter) {
|
||||
[self.leftCard renderWithCharacter:self.secondCharacter rank:2];
|
||||
} else {
|
||||
[self.leftCard renderWithCharacter:[KBCharacter new] rank:2];
|
||||
}
|
||||
|
||||
if (first) [self.centerCard renderWithCharacter:first rank:1];
|
||||
if (second) [self.leftCard renderWithCharacter:second rank:2];
|
||||
if (third) [self.rightCard renderWithCharacter:third rank:3];
|
||||
if (self.thirdCharacter) {
|
||||
[self.rightCard renderWithCharacter:self.thirdCharacter rank:3];
|
||||
} else {
|
||||
[self.rightCard renderWithCharacter:[KBCharacter new] rank:3];
|
||||
}
|
||||
|
||||
// 根据 added 状态刷新三个加号按钮的 UI 和可点击状态
|
||||
[self updatePlusButtonsState];
|
||||
}
|
||||
|
||||
- (void)onCenterPlusTappedInternal {
|
||||
if (self.onCenterPlusTapped) {
|
||||
self.onCenterPlusTapped(self.firstCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)onLeftPlusTappedInternal {
|
||||
if (self.onLeftPlusTapped) {
|
||||
self.onLeftPlusTapped(self.secondCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)onRightPlusTappedInternal {
|
||||
if (self.onRightPlusTapped) {
|
||||
self.onRightPlusTapped(self.thirdCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
/// 根据角色是否已添加,更新三个底部按钮的文案与状态
|
||||
- (void)updatePlusButtonsState {
|
||||
[self updatePlusButton:self.plusButton withCharacter:self.firstCharacter];
|
||||
[self updatePlusButton:self.leftPlusButton withCharacter:self.secondCharacter];
|
||||
[self updatePlusButton:self.rightPlusButton withCharacter:self.thirdCharacter];
|
||||
}
|
||||
|
||||
- (void)updatePlusButton:(UIButton *)button withCharacter:(KBCharacter * _Nullable)character {
|
||||
if (!character) {
|
||||
button.hidden = YES;
|
||||
return;
|
||||
}
|
||||
button.hidden = NO;
|
||||
|
||||
BOOL added = character.added;
|
||||
if (added) {
|
||||
// 已添加:灰色背景 + 勾选,且禁用点击
|
||||
[button setTitle:@"✓" forState:UIControlStateNormal];
|
||||
[button setTitleColor:[UIColor colorWithWhite:0.45 alpha:1.0] forState:UIControlStateNormal];
|
||||
button.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1.0];
|
||||
button.enabled = NO;
|
||||
} else {
|
||||
// 未添加:绿色加号,可点击
|
||||
[button setTitle:@"+" forState:UIControlStateNormal];
|
||||
[button setTitleColor:[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] forState:UIControlStateNormal];
|
||||
button.backgroundColor = [[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] colorWithAlphaComponent:0.15];
|
||||
button.enabled = YES;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Lazy
|
||||
@@ -235,6 +296,7 @@
|
||||
_plusButton.layer.masksToBounds = YES;
|
||||
// 默认浅色背景,可按需要在外层配置
|
||||
_plusButton.backgroundColor = [[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] colorWithAlphaComponent:0.15];
|
||||
[_plusButton addTarget:self action:@selector(onCenterPlusTappedInternal) forControlEvents:UIControlEventTouchUpInside];
|
||||
}
|
||||
return _plusButton;
|
||||
}
|
||||
@@ -248,6 +310,7 @@
|
||||
_leftPlusButton.layer.cornerRadius = 14.0;
|
||||
_leftPlusButton.layer.masksToBounds = YES;
|
||||
_leftPlusButton.backgroundColor = [[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] colorWithAlphaComponent:0.15];
|
||||
[_leftPlusButton addTarget:self action:@selector(onLeftPlusTappedInternal) forControlEvents:UIControlEventTouchUpInside];
|
||||
}
|
||||
return _leftPlusButton;
|
||||
}
|
||||
@@ -261,6 +324,7 @@
|
||||
_rightPlusButton.layer.cornerRadius = 14.0;
|
||||
_rightPlusButton.layer.masksToBounds = YES;
|
||||
_rightPlusButton.backgroundColor = [[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] colorWithAlphaComponent:0.15];
|
||||
[_rightPlusButton addTarget:self action:@selector(onRightPlusTappedInternal) forControlEvents:UIControlEventTouchUpInside];
|
||||
}
|
||||
return _rightPlusButton;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user