JXCategoryTitleView.m 8.1 KB

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