This commit is contained in:
2025-12-03 20:02:37 +08:00
parent f026b9f9fd
commit eca168957d
3 changed files with 33 additions and 30 deletions

View File

@@ -6,16 +6,16 @@
// //
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "KBCharacter.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
/// 用于 HomeHot 顶部展示前三名的视图 /// 用于 HomeHot 顶部展示前三名的视图
@interface KBTopThreeView : UIView @interface KBTopThreeView : UIView
/// 配置三项内容(数组元素可传 @{ @"title":NSString, @"rank":@(1/2/3) } /// 使用前三名角色模型进行配置(数组元素为 KBCharacter*,最多取前三个
- (void)configWithItems:(NSArray<NSDictionary *> *)items; - (void)configWithCharacters:(NSArray<KBCharacter *> *)characters;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@@ -4,17 +4,18 @@
// //
#import "KBTopThreeView.h" #import "KBTopThreeView.h"
#import "UIImageView+KBWebImage.h"
@interface KBTopThreeCardView : UIView @interface KBTopThreeCardView : UIView
/// ///
@property (nonatomic, strong) UIImageView *avatarCircleView; @property (nonatomic, strong) UIImageView *avatarCircleView;
/// ///
@property (nonatomic, strong) UIImageView *cardImageView; @property (nonatomic, strong) UIImageView *cardImageView;
/// ///
@property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *titleLabel;
/// /// 使
- (void)renderWithTitle:(NSString *)title rank:(NSInteger)rank; - (void)renderWithCharacter:(KBCharacter *)character rank:(NSInteger)rank;
@end @end
@implementation KBTopThreeCardView @implementation KBTopThreeCardView
@@ -55,7 +56,8 @@
} }
- (void)renderWithTitle:(NSString *)title rank:(NSInteger)rank { - (void)renderWithCharacter:(KBCharacter *)character rank:(NSInteger)rank {
NSString *title = character.characterName ?: @"";
self.titleLabel.text = title.length ? title : @""; self.titleLabel.text = title.length ? title : @"";
// //
@@ -80,7 +82,9 @@
} break; // } break; //
} }
self.cardImageView.image = image; self.cardImageView.image = image;
// KBTopThreeView
//
[self.avatarCircleView kb_setAvatarURL:character.avatarUrl placeholder:KBPlaceholderImage];
// //
self.avatarCircleView.layer.borderColor = [UIColor colorWithRed:0.83 green:0.95 blue:0.27 alpha:1.0].CGColor; self.avatarCircleView.layer.borderColor = [UIColor colorWithRed:0.83 green:0.95 blue:0.27 alpha:1.0].CGColor;
@@ -94,6 +98,7 @@
_avatarCircleView.backgroundColor = [UIColor whiteColor]; _avatarCircleView.backgroundColor = [UIColor whiteColor];
_avatarCircleView.layer.cornerRadius = 34; _avatarCircleView.layer.cornerRadius = 34;
_avatarCircleView.layer.borderWidth = 2.0; _avatarCircleView.layer.borderWidth = 2.0;
_avatarCircleView.layer.masksToBounds = true;
} }
return _avatarCircleView; return _avatarCircleView;
} }
@@ -197,19 +202,22 @@
}]; }];
} }
- (void)configWithItems:(NSArray<NSDictionary *> *)items { - (void)configWithCharacters:(NSArray<KBCharacter *> *)characters {
// items rank 1 2 3 if (characters.count == 0) {
NSString *t1 = @"", *t2 = @"", *t3 = @""; //
for (NSDictionary *d in items) { [self.centerCard renderWithCharacter:[KBCharacter new] rank:1];
NSInteger r = [d[@"rank"] integerValue]; [self.leftCard renderWithCharacter:[KBCharacter new] rank:2];
NSString *title = d[@"title"] ?: @""; [self.rightCard renderWithCharacter:[KBCharacter new] rank:3];
if (r == 1) t1 = title; return;
else if (r == 2) t2 = title;
else if (r == 3) t3 = title;
} }
[self.centerCard renderWithTitle:t1 rank:1];
[self.leftCard renderWithTitle:t2 rank:2]; KBCharacter *first = characters.count > 0 ? characters[0] : nil;
[self.rightCard renderWithTitle:t3 rank:3]; KBCharacter *second = characters.count > 1 ? characters[1] : nil;
KBCharacter *third = characters.count > 2 ? characters[2] : nil;
if (first) [self.centerCard renderWithCharacter:first rank:1];
if (second) [self.leftCard renderWithCharacter:second rank:2];
if (third) [self.rightCard renderWithCharacter:third rank:3];
} }
#pragma mark - Lazy #pragma mark - Lazy

View File

@@ -63,17 +63,12 @@
} }
weakSelf.allCharacters = list ?: @[]; weakSelf.allCharacters = list ?: @[];
// // KBTopThreeView
NSMutableArray *topItems = [NSMutableArray array];
NSInteger topCount = MIN(3, weakSelf.allCharacters.count); NSInteger topCount = MIN(3, weakSelf.allCharacters.count);
for (NSInteger i = 0; i < topCount; i++) { if (topCount > 0) {
KBCharacter *c = weakSelf.allCharacters[i]; NSRange range = NSMakeRange(0, topCount);
// NSInteger rank = (c.rank > 0) ? c.rank : (i + 1); NSArray<KBCharacter *> *topThree = [weakSelf.allCharacters subarrayWithRange:range];
NSString *title = c.characterName ?: @""; [weakSelf.topThreeView configWithCharacters:topThree];
[topItems addObject:@{ @"title": title, @"rank": @(i + 1) }];
}
if (topItems.count > 0) {
[weakSelf.topThreeView configWithItems:topItems];
} }
// 4 // 4