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

188 lines
6.1 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// HomeHotCell.m
// keyBoard
//
#import "HomeHotCell.h"
#import "UIImageView+KBWebImage.h"
@interface HomeHotCell()
// 左侧序号
@property (nonatomic, strong) UILabel *rankLabel;
@property (nonatomic, strong) UIView *yuanView;
// 左侧圆形头像
@property (nonatomic, strong) UIImageView *avatarView;
// 标题
@property (nonatomic, strong) UILabel *titleLabel;
// 副标题
@property (nonatomic, strong) UILabel *subLabel;
// 右侧加入按钮(或已加入勾选)
@property (nonatomic, strong) UIButton *actionButton;
@end
@implementation HomeHotCell
- (void)setupUI {
[super setupUI];
// 添加控件
[self.contentView addSubview:self.yuanView];
[self.contentView addSubview:self.rankLabel];
[self.contentView addSubview:self.avatarView];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.subLabel];
[self.contentView addSubview:self.actionButton];
// 布局(使用 Masonry
[self.rankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(16);
make.centerY.equalTo(self.contentView);
}];
[self.yuanView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.rankLabel).offset(2);
make.bottom.equalTo(self.rankLabel).offset(-3);
make.width.height.mas_equalTo(6);
}];
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.rankLabel.mas_right).offset(12);
make.centerY.equalTo(self.contentView);
make.width.height.mas_equalTo(56);
}];
[self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.contentView);
make.right.equalTo(self.contentView).offset(-16);
make.width.mas_equalTo(56);
make.height.mas_equalTo(38);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.avatarView.mas_right).offset(12);
make.top.equalTo(self.avatarView.mas_top).offset(-2);
make.right.lessThanOrEqualTo(self.actionButton.mas_left).offset(-12);
}];
[self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.titleLabel);
make.right.equalTo(self.titleLabel);
make.bottom.equalTo(self.avatarView.mas_bottom).offset(2);
}];
}
- (void)actionButtonTapped {
if (self.onTapAction) {
self.onTapAction();
}
}
- (void)setCharacter:(KBCharacter *)character {
_character = character;
// 基本文案
NSInteger rank = character.rank;
self.rankLabel.text = rank > 0 ? [NSString stringWithFormat:@"%ld", (long)rank] : @"";
self.titleLabel.text = character.characterName ?: @"";
self.subLabel.text = character.download ?: @"";
[self.avatarView kb_setAvatarURL:character.avatarUrl placeholder:KBAvatarPlaceholderImage];
// 加入状态
BOOL joined = character.added;
if (joined) {
// 已加入状态:灰背景、打勾
[self.actionButton setTitle:@"" forState:UIControlStateNormal];
[self.actionButton setTitleColor:[UIColor colorWithWhite:0.45 alpha:1] forState:UIControlStateNormal];
self.actionButton.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
} else {
// 可加入状态:绿色加号
[self.actionButton setTitle:@"+" forState:UIControlStateNormal];
[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;
}
// 兼容旧调用:转成一个临时模型再走统一逻辑
- (void)configWithRank:(NSInteger)rank title:(NSString *)title subtitle:(NSString *)sub joined:(BOOL)joined {
KBCharacter *c = [KBCharacter new];
c.rank = rank;
c.characterName = title;
c.download = sub;
c.added = joined;
self.character = c;
}
- (void)prepareForReuse {
[super prepareForReuse];
[self.avatarView kb_cancelImageLoad];
self.avatarView.image = nil;
self.character = nil;
self.onTapAction = nil;
}
#pragma mark - Lazy
- (UILabel *)rankLabel {
if (!_rankLabel) {
_rankLabel = [[UILabel alloc] init];
_rankLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
_rankLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _rankLabel;
}
- (UIImageView *)avatarView {
if (!_avatarView) {
_avatarView = [[UIImageView alloc] init];
_avatarView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
_avatarView.layer.cornerRadius = 28;
_avatarView.layer.masksToBounds = YES;
_avatarView.contentMode = UIViewContentModeScaleAspectFill;
}
return _avatarView;
}
- (UIView *)yuanView {
if (!_yuanView) {
_yuanView = [[UIView alloc] init];
_yuanView.backgroundColor = [UIColor colorWithHex:0x26D3AA];
_yuanView.layer.cornerRadius = 3;
_yuanView.layer.masksToBounds = YES;
}
return _yuanView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [KBFont medium:16];
_titleLabel.textColor = [UIColor colorWithHex:0x1B1F1A];
}
return _titleLabel;
}
- (UILabel *)subLabel {
if (!_subLabel) {
_subLabel = [[UILabel alloc] init];
_subLabel.font = [KBFont regular:12];
_subLabel.textColor = [UIColor colorWithHex:0x9A9A9A];
_subLabel.numberOfLines = 2;
}
return _subLabel;
}
- (UIButton *)actionButton {
if (!_actionButton) {
_actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
_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;
}
@end