// // HomeRankVC.m // keyBoard // // Created by Mac on 2025/11/6. // #import "HomeRankVC.h" #import "HomeRankContentVC.h" #import "KBCategoryTitleImageView.h" #import "KBHomeVM.h" #import "KBTag.h" @interface HomeRankVC () @property (nonatomic, strong) NSArray *titles; @property (nonatomic, strong) NSArray *tags; @property (nonatomic, strong) JXCategoryTitleImageView *myCategoryView; @property (nonatomic, strong) JXCategoryListContainerView *listContainerView; @property (nonatomic, assign) JXCategoryTitleImageType currentType; @property (nonatomic, strong) KBHomeVM *homeVM; @end @implementation HomeRankVC - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor clearColor]; self.titles = @[]; [self.view addSubview:self.myCategoryView]; [self.view addSubview:self.listContainerView]; self.listContainerView.scrollView.scrollEnabled = false; // 静态样式配置(与数据无关) self.myCategoryView.cellBackgroundSelectedColor = [UIColor blackColor]; self.myCategoryView.cellBackgroundUnselectedColor = [UIColor colorWithHex:0xEAF8F4]; self.myCategoryView.titleColor = [UIColor colorWithHex:0x1B1F1A]; self.myCategoryView.titleSelectedColor = [UIColor whiteColor]; self.myCategoryView.titleFont = [UIFont systemFontOfSize:12]; // 关闭各种“放大”效果,保持选中与未选中宽度一致 self.myCategoryView.titleLabelZoomEnabled = NO; // 关闭文字缩放 self.myCategoryView.titleLabelZoomScale = 1.0; self.myCategoryView.cellWidthZoomEnabled = NO; // 关闭cell宽度缩放 self.myCategoryView.cellWidthZoomScale = 1.0; self.myCategoryView.imageZoomEnabled = false; self.myCategoryView.imageZoomScale = 1.3; self.myCategoryView.averageCellSpacingEnabled = NO; self.myCategoryView.cellBackgroundColorGradientEnabled = true; self.myCategoryView.cellWidthIncrement = 20; // 加载标签列表 self.homeVM = [KBHomeVM new]; KBWeakSelf [self.homeVM fetchTagListWithParams:nil needShow:YES completion:^(NSArray * _Nullable list, NSError * _Nullable error) { if (error) { return; } weakSelf.tags = list ?: @[]; // titles 使用后端返回的 tagName(不再本地化) NSMutableArray *titles = [NSMutableArray array]; for (KBTag *tag in weakSelf.tags) { NSString *name = tag.tagName ?: @""; [titles addObject:name]; } weakSelf.titles = [titles copy]; // 根据返回数量构造图片数组 NSMutableArray *imageNames = [NSMutableArray array]; NSMutableArray *selectedImageNames = [NSMutableArray array]; for (NSInteger i = 0; i < weakSelf.titles.count; i++) { [imageNames addObject:@"home_item_normal"]; [selectedImageNames addObject:@"home_item_selected"]; } weakSelf.myCategoryView.titles = weakSelf.titles; weakSelf.myCategoryView.imageInfoArray = imageNames; weakSelf.myCategoryView.selectedImageInfoArray = selectedImageNames; weakSelf.myCategoryView.loadImageBlock = ^(UIImageView *imageView, id info) { NSString *imageName = info; imageView.image = [UIImage imageNamed:imageName]; }; [weakSelf.myCategoryView reloadData]; [weakSelf.listContainerView reloadData]; }]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.myCategoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, [self preferredCategoryViewHeight]); self.listContainerView.frame = CGRectMake(0, [self preferredCategoryViewHeight], self.view.bounds.size.width, self.view.bounds.size.height - [self preferredCategoryViewHeight]); } //- (JXCategoryBaseView *)preferredCategoryView { // return [[JXCategoryBaseView alloc] init]; //} - (CGFloat)preferredCategoryViewHeight { return 30; } - (void)configCategoryViewWithType:(JXCategoryTitleImageType)imageType { self.currentType = imageType; if ((NSInteger)imageType == 100) { NSMutableArray *types = [NSMutableArray array]; for (int i = 0; i < self.titles.count; i++) { if (i == 2) { [types addObject:@(JXCategoryTitleImageType_OnlyImage)]; }else if (i == 4) { [types addObject:@(JXCategoryTitleImageType_LeftImage)]; }else { [types addObject:@(JXCategoryTitleImageType_OnlyTitle)]; } } self.myCategoryView.imageTypes = types; }else { NSMutableArray *types = [NSMutableArray array]; for (int i = 0; i < self.titles.count; i++) { [types addObject:@(imageType)]; } self.myCategoryView.imageTypes = types; } [self.myCategoryView reloadData]; } // 分页菜单视图 - (JXCategoryTitleImageView *)myCategoryView { if (!_myCategoryView) { // Use our subclass so cells get a 4pt rounded background locally _myCategoryView = (JXCategoryTitleImageView *)[[KBCategoryTitleImageView alloc] init]; _myCategoryView.delegate = self; // !!!: 将列表容器视图关联到 categoryView _myCategoryView.listContainer = self.listContainerView; } return _myCategoryView; } // 列表容器视图 - (JXCategoryListContainerView *)listContainerView { if (!_listContainerView) { _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self]; } return _listContainerView; } #pragma mark - JXCategoryViewDelegate // 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,不关心具体是点击还是滚动选中的。 - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index { NSLog(@"%@", NSStringFromSelector(_cmd)); // 拿到当前显示的列表 VC(已创建/显示过的会记录在 validListDict 中) id list = self.listContainerView.validListDict[@(index)]; if ([list isKindOfClass:[HomeRankContentVC class]]) { HomeRankContentVC *currentVC = (HomeRankContentVC *)list; // 如需拿到对应的 collectionView,可以在这里同步一下 self.collectionView = currentVC.collectionView; // TODO: 在这里对 currentVC 做需要的操作 } // 侧滑手势处理 self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0); } // 滚动选中的情况才会调用该方法 - (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index { NSLog(@"%@", NSStringFromSelector(_cmd)); } #pragma mark - JXCategoryListContainerViewDelegate // 返回列表的数量 - (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView { return self.titles.count; } // 返回各个列表菜单下的实例,该实例需要遵守并实现 协议 - (id)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index { KBTag *tagModel = self.tags[index]; HomeRankContentVC *list = [[HomeRankContentVC alloc] init]; list.tagModel = tagModel; self.collectionView = list.collectionView; return list; } #pragma mark - TitleImageSettingViewControllerDelegate - (void)titleImageSettingVCDidSelectedImageType:(JXCategoryTitleImageType)imageType { [self configCategoryViewWithType:imageType]; } @end