JXCategoryTitleView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // JXCategoryView.m
  3. // UI系列测试
  4. //
  5. // Created by jiaxin on 2018/3/15.
  6. // Copyright © 2018年 jiaxin. All rights reserved.
  7. //
  8. #import "JXCategoryTitleView.h"
  9. #import "JXCategoryFactory.h"
  10. @implementation JXCategoryTitleView
  11. - (void)initializeData {
  12. [super initializeData];
  13. _titleNumberOfLines = 1;
  14. _titleLabelZoomEnabled = NO;
  15. _titleLabelZoomScale = 1.2;
  16. _titleColor = [UIColor blackColor];
  17. _titleSelectedColor = [UIColor redColor];
  18. _titleFont = [UIFont systemFontOfSize:15];
  19. _titleColorGradientEnabled = NO;
  20. _titleLabelMaskEnabled = NO;
  21. _titleLabelZoomScrollGradientEnabled = YES;
  22. _titleLabelStrokeWidthEnabled = NO;
  23. _titleLabelSelectedStrokeWidth = -3;
  24. _titleLabelVerticalOffset = 0;
  25. _titleLabelAnchorPointStyle = JXCategoryTitleLabelAnchorPointStyleCenter;
  26. }
  27. - (UIFont *)titleSelectedFont {
  28. if (_titleSelectedFont) {
  29. return _titleSelectedFont;
  30. }
  31. return self.titleFont;
  32. }
  33. #pragma mark - Override
  34. - (Class)preferredCellClass {
  35. return [JXCategoryTitleCell class];
  36. }
  37. - (void)refreshDataSource {
  38. NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:self.titles.count];
  39. for (int i = 0; i < self.titles.count; i++) {
  40. JXCategoryTitleCellModel *cellModel = [[JXCategoryTitleCellModel alloc] init];
  41. [tempArray addObject:cellModel];
  42. }
  43. self.dataSource = [NSArray arrayWithArray:tempArray];
  44. }
  45. - (void)refreshSelectedCellModel:(JXCategoryBaseCellModel *)selectedCellModel unselectedCellModel:(JXCategoryBaseCellModel *)unselectedCellModel {
  46. [super refreshSelectedCellModel:selectedCellModel unselectedCellModel:unselectedCellModel];
  47. JXCategoryTitleCellModel *myUnselectedCellModel = (JXCategoryTitleCellModel *)unselectedCellModel;
  48. JXCategoryTitleCellModel *myselectedCellModel = (JXCategoryTitleCellModel *)selectedCellModel;
  49. if (self.isSelectedAnimationEnabled && (selectedCellModel.selectedType == JXCategoryCellSelectedTypeClick || selectedCellModel.selectedType == JXCategoryCellSelectedTypeCode)) {
  50. //开启了动画过渡,且cell在屏幕内,current的属性值会在cell里面进行动画插值更新
  51. //1、当unselectedCell在屏幕外的时候,还是需要在这里更新值
  52. //2、当selectedCell在屏幕外的时候,还是需要在这里更新值(比如调用selectItemAtIndex方法选中的时候)
  53. BOOL isUnselectedCellVisible = NO;
  54. BOOL isSelectedCellVisible = NO;
  55. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  56. for (NSIndexPath *indexPath in indexPaths) {
  57. if (indexPath.item == myUnselectedCellModel.index) {
  58. isUnselectedCellVisible = YES;
  59. continue;
  60. } else if (indexPath.item == myselectedCellModel.index) {
  61. isSelectedCellVisible = YES;
  62. continue;
  63. }
  64. }
  65. if (!isUnselectedCellVisible) {
  66. //但是当unselectedCell在屏幕外时,不会在cell里面通过动画插值更新,在这里直接更新
  67. myUnselectedCellModel.titleCurrentColor = myUnselectedCellModel.titleNormalColor;
  68. myUnselectedCellModel.titleLabelCurrentZoomScale = myUnselectedCellModel.titleLabelNormalZoomScale;
  69. myUnselectedCellModel.titleLabelCurrentStrokeWidth = myUnselectedCellModel.titleLabelNormalStrokeWidth;
  70. }
  71. if (!isSelectedCellVisible) {
  72. //但是当selectedCell在屏幕外时,不会在cell里面通过动画插值更新,在这里直接更新
  73. myselectedCellModel.titleCurrentColor = myselectedCellModel.titleSelectedColor;
  74. myselectedCellModel.titleLabelCurrentZoomScale = myselectedCellModel.titleLabelSelectedZoomScale;
  75. myselectedCellModel.titleLabelCurrentStrokeWidth = myselectedCellModel.titleLabelSelectedStrokeWidth;
  76. }
  77. } else {
  78. //没有开启动画,可以直接更新属性
  79. myselectedCellModel.titleCurrentColor = myselectedCellModel.titleSelectedColor;
  80. myselectedCellModel.titleLabelCurrentZoomScale = myselectedCellModel.titleLabelSelectedZoomScale;
  81. myselectedCellModel.titleLabelCurrentStrokeWidth = myselectedCellModel.titleLabelSelectedStrokeWidth;
  82. myUnselectedCellModel.titleCurrentColor = myUnselectedCellModel.titleNormalColor;
  83. myUnselectedCellModel.titleLabelCurrentZoomScale = myUnselectedCellModel.titleLabelNormalZoomScale;
  84. myUnselectedCellModel.titleLabelCurrentStrokeWidth = myUnselectedCellModel.titleLabelNormalStrokeWidth;
  85. }
  86. }
  87. - (void)refreshLeftCellModel:(JXCategoryBaseCellModel *)leftCellModel rightCellModel:(JXCategoryBaseCellModel *)rightCellModel ratio:(CGFloat)ratio {
  88. [super refreshLeftCellModel:leftCellModel rightCellModel:rightCellModel ratio:ratio];
  89. JXCategoryTitleCellModel *leftModel = (JXCategoryTitleCellModel *)leftCellModel;
  90. JXCategoryTitleCellModel *rightModel = (JXCategoryTitleCellModel *)rightCellModel;
  91. if (self.isTitleLabelZoomEnabled && self.isTitleLabelZoomScrollGradientEnabled) {
  92. leftModel.titleLabelCurrentZoomScale = [JXCategoryFactory interpolationFrom:self.titleLabelZoomScale to:1.0 percent:ratio];
  93. rightModel.titleLabelCurrentZoomScale = [JXCategoryFactory interpolationFrom:1.0 to:self.titleLabelZoomScale percent:ratio];
  94. }
  95. if (self.isTitleLabelStrokeWidthEnabled) {
  96. leftModel.titleLabelCurrentStrokeWidth = [JXCategoryFactory interpolationFrom:leftModel.titleLabelSelectedStrokeWidth to:leftModel.titleLabelNormalStrokeWidth percent:ratio];
  97. rightModel.titleLabelCurrentStrokeWidth = [JXCategoryFactory interpolationFrom:rightModel.titleLabelNormalStrokeWidth to:rightModel.titleLabelSelectedStrokeWidth percent:ratio];
  98. }
  99. if (self.isTitleColorGradientEnabled) {
  100. leftModel.titleCurrentColor = [JXCategoryFactory interpolationColorFrom:self.titleSelectedColor to:self.titleColor percent:ratio];
  101. rightModel.titleCurrentColor = [JXCategoryFactory interpolationColorFrom:self.titleColor to:self.titleSelectedColor percent:ratio];
  102. }
  103. }
  104. - (CGFloat)preferredCellWidthAtIndex:(NSInteger)index {
  105. if (self.cellWidth == JXCategoryViewAutomaticDimension) {
  106. if (self.titleDataSource && [self.titleDataSource respondsToSelector:@selector(categoryTitleView:widthForTitle:)]) {
  107. return [self.titleDataSource categoryTitleView:self widthForTitle:self.titles[index]];
  108. } else {
  109. return ceilf([self.titles[index] boundingRectWithSize:CGSizeMake(MAXFLOAT, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.titleFont} context:nil].size.width);
  110. }
  111. } else {
  112. return self.cellWidth;
  113. }
  114. }
  115. - (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index {
  116. [super refreshCellModel:cellModel index:index];
  117. JXCategoryTitleCellModel *model = (JXCategoryTitleCellModel *)cellModel;
  118. model.title = self.titles[index];
  119. model.titleNumberOfLines = self.titleNumberOfLines;
  120. model.titleFont = self.titleFont;
  121. model.titleSelectedFont = self.titleSelectedFont;
  122. model.titleNormalColor = self.titleColor;
  123. model.titleSelectedColor = self.titleSelectedColor;
  124. model.titleLabelMaskEnabled = self.isTitleLabelMaskEnabled;
  125. model.titleLabelZoomEnabled = self.isTitleLabelZoomEnabled;
  126. model.titleLabelNormalZoomScale = 1;
  127. model.titleLabelZoomSelectedVerticalOffset = self.titleLabelZoomSelectedVerticalOffset;
  128. model.titleLabelSelectedZoomScale = self.titleLabelZoomScale;
  129. model.titleLabelStrokeWidthEnabled = self.isTitleLabelStrokeWidthEnabled;
  130. model.titleLabelNormalStrokeWidth = 0;
  131. model.titleLabelSelectedStrokeWidth = self.titleLabelSelectedStrokeWidth;
  132. model.titleLabelVerticalOffset = self.titleLabelVerticalOffset;
  133. model.titleLabelAnchorPointStyle = self.titleLabelAnchorPointStyle;
  134. if (index == self.selectedIndex) {
  135. model.titleCurrentColor = model.titleSelectedColor;
  136. model.titleLabelCurrentZoomScale = model.titleLabelSelectedZoomScale;
  137. model.titleLabelCurrentStrokeWidth= model.titleLabelSelectedStrokeWidth;
  138. }else {
  139. model.titleCurrentColor = model.titleNormalColor;
  140. model.titleLabelCurrentZoomScale = model.titleLabelNormalZoomScale;
  141. model.titleLabelCurrentStrokeWidth = model.titleLabelNormalStrokeWidth;
  142. }
  143. }
  144. @end