Files
keyboard/keyBoard/Class/Home/V/KBTopThreeView.m

345 lines
13 KiB
Mathematica
Raw Normal View History

2025-11-06 14:59:00 +08:00
//
// KBTopThreeView.m
// keyBoard
//
#import "KBTopThreeView.h"
2025-12-03 20:02:37 +08:00
#import "UIImageView+KBWebImage.h"
2025-11-06 14:59:00 +08:00
@interface KBTopThreeCardView : UIView
2025-12-03 20:02:37 +08:00
///
2025-12-03 19:50:23 +08:00
@property (nonatomic, strong) UIImageView *avatarCircleView;
2025-11-06 14:59:00 +08:00
///
2025-11-07 19:33:54 +08:00
@property (nonatomic, strong) UIImageView *cardImageView;
2025-11-06 14:59:00 +08:00
///
@property (nonatomic, strong) UILabel *titleLabel;
2025-12-03 20:02:37 +08:00
/// 使
- (void)renderWithCharacter:(KBCharacter *)character rank:(NSInteger)rank;
2025-11-06 14:59:00 +08:00
@end
@implementation KBTopThreeCardView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
}
return self;
}
- (void)setupUI {
//
2025-11-07 19:33:54 +08:00
[self addSubview:self.cardImageView];
2025-11-06 14:59:00 +08:00
//
[self addSubview:self.avatarCircleView];
2025-11-07 19:33:54 +08:00
[self.cardImageView addSubview:self.titleLabel];
2025-11-06 14:59:00 +08:00
//
[self.avatarCircleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self);
make.centerX.equalTo(self);
make.width.height.mas_equalTo(68);
}];
2025-11-07 19:33:54 +08:00
[self.cardImageView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-06 14:59:00 +08:00
make.top.equalTo(self.avatarCircleView.mas_bottom).offset(-34); //
make.left.right.equalTo(self);
2025-11-07 20:58:14 +08:00
make.height.mas_equalTo(KBFit(158));
2025-11-06 14:59:00 +08:00
}];
2025-11-07 19:33:54 +08:00
2025-11-06 14:59:00 +08:00
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-07 19:33:54 +08:00
make.centerX.equalTo(self.cardImageView);
make.top.equalTo(self.cardImageView).offset(KBFit(95));
2025-11-06 14:59:00 +08:00
}];
}
2025-12-03 20:02:37 +08:00
- (void)renderWithCharacter:(KBCharacter *)character rank:(NSInteger)rank {
NSString *title = character.characterName ?: @"";
2025-11-06 14:59:00 +08:00
self.titleLabel.text = title.length ? title : @"";
//
UIColor *theme;
2025-11-07 19:33:54 +08:00
UIImage *image;
2025-11-06 14:59:00 +08:00
switch (rank) {
2025-11-07 19:33:54 +08:00
case 1:
{
theme = [UIColor colorWithRed:1.00 green:0.86 blue:0.43 alpha:1.0];
image = [UIImage imageNamed:@"home_rank_1"];
}
break; //
case 2: {
theme = [UIColor colorWithRed:0.68 green:0.80 blue:1.00 alpha:1.0];
image = [UIImage imageNamed:@"home_rank_2"];
}
break; //
default: {
theme = [UIColor colorWithRed:1.00 green:0.78 blue:0.63 alpha:1.0];
image = [UIImage imageNamed:@"home_rank_3"];
} break; //
2025-11-06 14:59:00 +08:00
}
2025-11-07 19:33:54 +08:00
self.cardImageView.image = image;
2025-12-03 20:02:37 +08:00
//
[self.avatarCircleView kb_setAvatarURL:character.avatarUrl placeholder:KBAvatarPlaceholderImage];
2025-11-06 14:59:00 +08:00
//
self.avatarCircleView.layer.borderColor = [UIColor colorWithRed:0.83 green:0.95 blue:0.27 alpha:1.0].CGColor;
}
#pragma mark - Lazy
2025-12-03 19:50:23 +08:00
- (UIImageView *)avatarCircleView {
2025-11-06 14:59:00 +08:00
if (!_avatarCircleView) {
2025-12-03 19:50:23 +08:00
_avatarCircleView = [[UIImageView alloc] init];
2025-11-06 14:59:00 +08:00
_avatarCircleView.backgroundColor = [UIColor whiteColor];
_avatarCircleView.layer.cornerRadius = 34;
_avatarCircleView.layer.borderWidth = 2.0;
2025-12-03 20:02:37 +08:00
_avatarCircleView.layer.masksToBounds = true;
2025-11-06 14:59:00 +08:00
}
return _avatarCircleView;
}
2025-11-07 19:33:54 +08:00
- (UIImageView *)cardImageView {
if (!_cardImageView) {
_cardImageView = [[UIImageView alloc] init];
2025-11-06 14:59:00 +08:00
}
2025-11-07 19:33:54 +08:00
return _cardImageView;
2025-11-06 14:59:00 +08:00
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.textColor = [UIColor colorWithWhite:0.1 alpha:1];
2025-11-25 15:36:16 +08:00
_titleLabel.font = [KBFont medium:13];
2025-11-06 14:59:00 +08:00
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
2025-11-07 19:55:11 +08:00
// plusButton
2025-11-06 14:59:00 +08:00
@end
@interface KBTopThreeView()
@property (nonatomic, strong) KBTopThreeCardView *leftCard;
@property (nonatomic, strong) KBTopThreeCardView *centerCard;
@property (nonatomic, strong) KBTopThreeCardView *rightCard;
2025-11-07 19:55:11 +08:00
/// KBTopThreeView
@property (nonatomic, strong) UIButton *plusButton;
2025-11-07 20:58:14 +08:00
/// plus
@property (nonatomic, strong) UIButton *leftPlusButton;
@property (nonatomic, strong) UIButton *rightPlusButton;
2025-12-04 19:12:34 +08:00
///
@property (nonatomic, strong, nullable) KBCharacter *firstCharacter;
@property (nonatomic, strong, nullable) KBCharacter *secondCharacter;
@property (nonatomic, strong, nullable) KBCharacter *thirdCharacter;
2025-11-06 14:59:00 +08:00
@end
@implementation KBTopThreeView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
2025-11-07 19:33:54 +08:00
self.backgroundColor = [UIColor whiteColor];
2025-11-06 14:59:00 +08:00
[self setupUI];
}
return self;
}
- (void)setupUI {
[self addSubview:self.leftCard];
[self addSubview:self.centerCard];
[self addSubview:self.rightCard];
2025-11-07 19:55:11 +08:00
[self addSubview:self.plusButton];
2025-11-07 20:58:14 +08:00
[self addSubview:self.leftPlusButton];
[self addSubview:self.rightPlusButton];
2025-11-06 14:59:00 +08:00
//
[self.centerCard mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self).offset(8);
2025-11-07 19:55:11 +08:00
make.width.mas_equalTo(KBFit(96));
make.height.mas_equalTo(KBFit(180));
2025-11-06 14:59:00 +08:00
}];
2025-11-07 19:55:11 +08:00
//
CGFloat sideTopDelta = 12.0; // 12pt
2025-11-06 14:59:00 +08:00
[self.leftCard mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-07 19:55:11 +08:00
make.top.equalTo(self.centerCard).offset(sideTopDelta);
2025-11-06 14:59:00 +08:00
make.right.equalTo(self.centerCard.mas_left).offset(-8);
make.width.equalTo(self.centerCard);
2025-11-07 19:55:11 +08:00
//
make.height.equalTo(self.centerCard);
2025-11-06 14:59:00 +08:00
}];
[self.rightCard mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-07 19:55:11 +08:00
make.top.equalTo(self.centerCard).offset(sideTopDelta);
2025-11-06 14:59:00 +08:00
make.left.equalTo(self.centerCard.mas_right).offset(8);
make.width.equalTo(self.centerCard);
2025-11-07 19:55:11 +08:00
make.height.equalTo(self.centerCard);
2025-11-06 14:59:00 +08:00
}];
2025-11-07 19:55:11 +08:00
// plusButton
[self.plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.centerCard.mas_bottom).offset(12);
2025-11-07 20:58:14 +08:00
make.centerX.equalTo(self); // equalTo(self.centerCard)
make.width.mas_equalTo(60);
2025-11-07 19:55:11 +08:00
make.height.mas_equalTo(28);
make.bottom.equalTo(self).offset(-8);
}];
2025-11-07 20:58:14 +08:00
// plus
[self.leftPlusButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leftCard.mas_bottom).offset(12);
make.centerX.equalTo(self.leftCard);
make.width.height.equalTo(self.plusButton);
make.bottom.equalTo(self).offset(-8);
}];
[self.rightPlusButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.rightCard.mas_bottom).offset(12);
make.centerX.equalTo(self.rightCard);
make.width.height.equalTo(self.plusButton);
make.bottom.equalTo(self).offset(-8);
}];
2025-11-06 14:59:00 +08:00
}
2025-12-03 20:02:37 +08:00
- (void)configWithCharacters:(NSArray<KBCharacter *> *)characters {
2025-12-04 19:12:34 +08:00
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 {
2025-12-03 20:02:37 +08:00
[self.centerCard renderWithCharacter:[KBCharacter new] rank:1];
2025-12-04 19:12:34 +08:00
}
if (self.secondCharacter) {
[self.leftCard renderWithCharacter:self.secondCharacter rank:2];
} else {
2025-12-03 20:02:37 +08:00
[self.leftCard renderWithCharacter:[KBCharacter new] rank:2];
2025-12-04 19:12:34 +08:00
}
if (self.thirdCharacter) {
[self.rightCard renderWithCharacter:self.thirdCharacter rank:3];
} else {
2025-12-03 20:02:37 +08:00
[self.rightCard renderWithCharacter:[KBCharacter new] rank:3];
2025-11-06 14:59:00 +08:00
}
2025-12-03 20:02:37 +08:00
2025-12-04 19:12:34 +08:00
// added UI
[self updatePlusButtonsState];
}
2025-12-03 20:02:37 +08:00
2025-12-04 19:12:34 +08:00
- (void)onCenterPlusTappedInternal {
2025-12-19 13:34:58 +08:00
if (![KBUserSessionManager shared].isLoggedIn) {
[[KBUserSessionManager shared] goLoginVC];
return;
}
2025-12-04 19:12:34 +08:00
if (self.onCenterPlusTapped) {
self.onCenterPlusTapped(self.firstCharacter);
}
}
- (void)onLeftPlusTappedInternal {
2025-12-19 13:34:58 +08:00
if (![KBUserSessionManager shared].isLoggedIn) {
[[KBUserSessionManager shared] goLoginVC];
return;
}
2025-12-04 19:12:34 +08:00
if (self.onLeftPlusTapped) {
self.onLeftPlusTapped(self.secondCharacter);
}
}
- (void)onRightPlusTappedInternal {
2025-12-19 13:34:58 +08:00
if (![KBUserSessionManager shared].isLoggedIn) {
[[KBUserSessionManager shared] goLoginVC];
return;
}
2025-12-04 19:12:34 +08:00
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;
}
2025-11-06 14:59:00 +08:00
}
#pragma mark - Lazy
- (KBTopThreeCardView *)leftCard { if (!_leftCard) _leftCard = [KBTopThreeCardView new]; return _leftCard; }
- (KBTopThreeCardView *)centerCard { if (!_centerCard) _centerCard = [KBTopThreeCardView new]; return _centerCard; }
- (KBTopThreeCardView *)rightCard { if (!_rightCard) _rightCard = [KBTopThreeCardView new]; return _rightCard; }
2025-11-07 19:55:11 +08:00
- (UIButton *)plusButton {
if (!_plusButton) {
_plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_plusButton setTitle:@"+" forState:UIControlStateNormal];
[_plusButton setTitleColor:[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] forState:UIControlStateNormal];
_plusButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
_plusButton.layer.cornerRadius = 14.0;
_plusButton.layer.masksToBounds = YES;
//
_plusButton.backgroundColor = [[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] colorWithAlphaComponent:0.15];
2025-12-04 19:12:34 +08:00
[_plusButton addTarget:self action:@selector(onCenterPlusTappedInternal) forControlEvents:UIControlEventTouchUpInside];
2025-11-07 19:55:11 +08:00
}
return _plusButton;
}
2025-11-06 14:59:00 +08:00
2025-11-07 20:58:14 +08:00
- (UIButton *)leftPlusButton {
if (!_leftPlusButton) {
_leftPlusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftPlusButton setTitle:@"+" forState:UIControlStateNormal];
[_leftPlusButton setTitleColor:[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] forState:UIControlStateNormal];
_leftPlusButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
_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];
2025-12-04 19:12:34 +08:00
[_leftPlusButton addTarget:self action:@selector(onLeftPlusTappedInternal) forControlEvents:UIControlEventTouchUpInside];
2025-11-07 20:58:14 +08:00
}
return _leftPlusButton;
}
- (UIButton *)rightPlusButton {
if (!_rightPlusButton) {
_rightPlusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rightPlusButton setTitle:@"+" forState:UIControlStateNormal];
[_rightPlusButton setTitleColor:[UIColor colorWithRed:0.20 green:0.65 blue:0.50 alpha:1.0] forState:UIControlStateNormal];
_rightPlusButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
_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];
2025-12-04 19:12:34 +08:00
[_rightPlusButton addTarget:self action:@selector(onRightPlusTappedInternal) forControlEvents:UIControlEventTouchUpInside];
2025-11-07 20:58:14 +08:00
}
return _rightPlusButton;
}
2025-11-07 19:55:11 +08:00
@end