Files
featherVoice/Pods/JXCategoryView/Sources/Title/JXCategoryTitleView.m

165 lines
8.3 KiB
Mathematica
Raw Normal View History

2025-08-08 10:49:36 +08:00
//
// JXCategoryView.m
// UI
//
// Created by jiaxin on 2018/3/15.
// Copyright © 2018 jiaxin. All rights reserved.
//
#import "JXCategoryTitleView.h"
#import "JXCategoryFactory.h"
@implementation JXCategoryTitleView
- (void)initializeData {
[super initializeData];
_titleNumberOfLines = 1;
_titleLabelZoomEnabled = NO;
_titleLabelZoomScale = 1.2;
_titleColor = [UIColor blackColor];
_titleSelectedColor = [UIColor redColor];
_titleFont = [UIFont systemFontOfSize:15];
_titleColorGradientEnabled = NO;
_titleLabelMaskEnabled = NO;
_titleLabelZoomScrollGradientEnabled = YES;
_titleLabelStrokeWidthEnabled = NO;
_titleLabelSelectedStrokeWidth = -3;
_titleLabelVerticalOffset = 0;
_titleLabelAnchorPointStyle = JXCategoryTitleLabelAnchorPointStyleCenter;
}
- (UIFont *)titleSelectedFont {
if (_titleSelectedFont) {
return _titleSelectedFont;
}
return self.titleFont;
}
#pragma mark - Override
- (Class)preferredCellClass {
return [JXCategoryTitleCell class];
}
- (void)refreshDataSource {
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:self.titles.count];
for (int i = 0; i < self.titles.count; i++) {
JXCategoryTitleCellModel *cellModel = [[JXCategoryTitleCellModel alloc] init];
[tempArray addObject:cellModel];
}
self.dataSource = [NSArray arrayWithArray:tempArray];
}
- (void)refreshSelectedCellModel:(JXCategoryBaseCellModel *)selectedCellModel unselectedCellModel:(JXCategoryBaseCellModel *)unselectedCellModel {
[super refreshSelectedCellModel:selectedCellModel unselectedCellModel:unselectedCellModel];
JXCategoryTitleCellModel *myUnselectedCellModel = (JXCategoryTitleCellModel *)unselectedCellModel;
JXCategoryTitleCellModel *myselectedCellModel = (JXCategoryTitleCellModel *)selectedCellModel;
if (self.isSelectedAnimationEnabled && (selectedCellModel.selectedType == JXCategoryCellSelectedTypeClick || selectedCellModel.selectedType == JXCategoryCellSelectedTypeCode)) {
//cellcurrentcell
//1unselectedCell
//2selectedCellselectItemAtIndex
BOOL isUnselectedCellVisible = NO;
BOOL isSelectedCellVisible = NO;
NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
for (NSIndexPath *indexPath in indexPaths) {
if (indexPath.item == myUnselectedCellModel.index) {
isUnselectedCellVisible = YES;
continue;
} else if (indexPath.item == myselectedCellModel.index) {
isSelectedCellVisible = YES;
continue;
}
}
if (!isUnselectedCellVisible) {
//unselectedCellcell
myUnselectedCellModel.titleCurrentColor = myUnselectedCellModel.titleNormalColor;
myUnselectedCellModel.titleLabelCurrentZoomScale = myUnselectedCellModel.titleLabelNormalZoomScale;
myUnselectedCellModel.titleLabelCurrentStrokeWidth = myUnselectedCellModel.titleLabelNormalStrokeWidth;
}
if (!isSelectedCellVisible) {
//selectedCellcell
myselectedCellModel.titleCurrentColor = myselectedCellModel.titleSelectedColor;
myselectedCellModel.titleLabelCurrentZoomScale = myselectedCellModel.titleLabelSelectedZoomScale;
myselectedCellModel.titleLabelCurrentStrokeWidth = myselectedCellModel.titleLabelSelectedStrokeWidth;
}
} else {
//
myselectedCellModel.titleCurrentColor = myselectedCellModel.titleSelectedColor;
myselectedCellModel.titleLabelCurrentZoomScale = myselectedCellModel.titleLabelSelectedZoomScale;
myselectedCellModel.titleLabelCurrentStrokeWidth = myselectedCellModel.titleLabelSelectedStrokeWidth;
myUnselectedCellModel.titleCurrentColor = myUnselectedCellModel.titleNormalColor;
myUnselectedCellModel.titleLabelCurrentZoomScale = myUnselectedCellModel.titleLabelNormalZoomScale;
myUnselectedCellModel.titleLabelCurrentStrokeWidth = myUnselectedCellModel.titleLabelNormalStrokeWidth;
}
}
- (void)refreshLeftCellModel:(JXCategoryBaseCellModel *)leftCellModel rightCellModel:(JXCategoryBaseCellModel *)rightCellModel ratio:(CGFloat)ratio {
[super refreshLeftCellModel:leftCellModel rightCellModel:rightCellModel ratio:ratio];
JXCategoryTitleCellModel *leftModel = (JXCategoryTitleCellModel *)leftCellModel;
JXCategoryTitleCellModel *rightModel = (JXCategoryTitleCellModel *)rightCellModel;
if (self.isTitleLabelZoomEnabled && self.isTitleLabelZoomScrollGradientEnabled) {
leftModel.titleLabelCurrentZoomScale = [JXCategoryFactory interpolationFrom:self.titleLabelZoomScale to:1.0 percent:ratio];
rightModel.titleLabelCurrentZoomScale = [JXCategoryFactory interpolationFrom:1.0 to:self.titleLabelZoomScale percent:ratio];
}
if (self.isTitleLabelStrokeWidthEnabled) {
leftModel.titleLabelCurrentStrokeWidth = [JXCategoryFactory interpolationFrom:leftModel.titleLabelSelectedStrokeWidth to:leftModel.titleLabelNormalStrokeWidth percent:ratio];
rightModel.titleLabelCurrentStrokeWidth = [JXCategoryFactory interpolationFrom:rightModel.titleLabelNormalStrokeWidth to:rightModel.titleLabelSelectedStrokeWidth percent:ratio];
}
if (self.isTitleColorGradientEnabled) {
leftModel.titleCurrentColor = [JXCategoryFactory interpolationColorFrom:self.titleSelectedColor to:self.titleColor percent:ratio];
rightModel.titleCurrentColor = [JXCategoryFactory interpolationColorFrom:self.titleColor to:self.titleSelectedColor percent:ratio];
}
}
- (CGFloat)preferredCellWidthAtIndex:(NSInteger)index {
if (self.cellWidth == JXCategoryViewAutomaticDimension) {
if (self.titleDataSource && [self.titleDataSource respondsToSelector:@selector(categoryTitleView:widthForTitle:)]) {
return [self.titleDataSource categoryTitleView:self widthForTitle:self.titles[index]];
} else {
return ceilf([self.titles[index] boundingRectWithSize:CGSizeMake(MAXFLOAT, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.titleFont} context:nil].size.width);
}
} else {
return self.cellWidth;
}
}
- (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index {
[super refreshCellModel:cellModel index:index];
JXCategoryTitleCellModel *model = (JXCategoryTitleCellModel *)cellModel;
model.title = self.titles[index];
model.titleNumberOfLines = self.titleNumberOfLines;
model.titleFont = self.titleFont;
model.titleSelectedFont = self.titleSelectedFont;
model.titleNormalColor = self.titleColor;
model.titleSelectedColor = self.titleSelectedColor;
model.titleLabelMaskEnabled = self.isTitleLabelMaskEnabled;
model.titleLabelZoomEnabled = self.isTitleLabelZoomEnabled;
model.titleLabelNormalZoomScale = 1;
model.titleLabelZoomSelectedVerticalOffset = self.titleLabelZoomSelectedVerticalOffset;
model.titleLabelSelectedZoomScale = self.titleLabelZoomScale;
model.titleLabelStrokeWidthEnabled = self.isTitleLabelStrokeWidthEnabled;
model.titleLabelNormalStrokeWidth = 0;
model.titleLabelSelectedStrokeWidth = self.titleLabelSelectedStrokeWidth;
model.titleLabelVerticalOffset = self.titleLabelVerticalOffset;
model.titleLabelAnchorPointStyle = self.titleLabelAnchorPointStyle;
if (index == self.selectedIndex) {
model.titleCurrentColor = model.titleSelectedColor;
model.titleLabelCurrentZoomScale = model.titleLabelSelectedZoomScale;
model.titleLabelCurrentStrokeWidth= model.titleLabelSelectedStrokeWidth;
}else {
model.titleCurrentColor = model.titleNormalColor;
model.titleLabelCurrentZoomScale = model.titleLabelNormalZoomScale;
model.titleLabelCurrentStrokeWidth = model.titleLabelNormalStrokeWidth;
}
}
@end