JXCategoryTitleVerticalZoomView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // JXCategoryTitleVerticalZoomView.m
  3. // JXCategoryView
  4. //
  5. // Created by jiaxin on 2019/2/14.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. #import "JXCategoryTitleVerticalZoomView.h"
  9. #import "JXCategoryTitleVerticalZoomCellModel.h"
  10. #import "JXCategoryTitleVerticalZoomCell.h"
  11. #import "JXCategoryFactory.h"
  12. #import "JXCategoryViewAnimator.h"
  13. @interface JXCategoryTitleVerticalZoomView()
  14. @property (nonatomic, strong) JXCategoryViewAnimator *edgeLeftAnimator;
  15. @property (nonatomic, assign) BOOL horizontalZoomTransitionAnimating;
  16. @end
  17. @implementation JXCategoryTitleVerticalZoomView
  18. - (void)initializeData {
  19. [super initializeData];
  20. _currentVerticalScale = 2;
  21. _maxVerticalFontScale = 2;
  22. _minVerticalFontScale = 1.3;
  23. _maxVerticalContentEdgeInsetLeft = 30;
  24. self.cellSpacing = 30;
  25. self.contentEdgeInsetLeft = 30;
  26. _minVerticalContentEdgeInsetLeft = 15;
  27. self.titleLabelZoomScale = _currentVerticalScale;
  28. self.titleLabelZoomEnabled = YES;
  29. self.selectedAnimationEnabled = YES;
  30. _maxVerticalCellSpacing = 30;
  31. _minVerticalCellSpacing = 20;
  32. }
  33. - (void)listDidScrollWithVerticalHeightPercent:(CGFloat)percent {
  34. CGFloat currentScale = [JXCategoryFactory interpolationFrom:self.minVerticalFontScale to:self.maxVerticalFontScale percent:percent];
  35. BOOL shouldReloadData = NO;
  36. if (self.currentVerticalScale != currentScale) {
  37. //有变化才允许reloadData
  38. shouldReloadData = YES;
  39. }
  40. self.currentVerticalScale = currentScale;
  41. CGFloat contentEdgeInsetScale = self.currentVerticalScale/self.maxVerticalFontScale;
  42. if (self.selectedIndex == 0) {
  43. self.contentEdgeInsetLeft = self.maxVerticalContentEdgeInsetLeft*contentEdgeInsetScale;
  44. }else {
  45. self.contentEdgeInsetLeft = self.minVerticalContentEdgeInsetLeft*contentEdgeInsetScale;
  46. }
  47. self.cellSpacing = [JXCategoryFactory interpolationFrom:self.minVerticalCellSpacing to:self.maxVerticalCellSpacing percent:percent];
  48. if (shouldReloadData) {
  49. //只有参数有变化才reloadData
  50. [self reloadData];
  51. }
  52. }
  53. - (void)setCurrentVerticalScale:(CGFloat)currentVerticalScale {
  54. _currentVerticalScale = currentVerticalScale;
  55. self.titleLabelZoomScale = currentVerticalScale;
  56. }
  57. - (Class)preferredCellClass {
  58. return [JXCategoryTitleVerticalZoomCell class];
  59. }
  60. - (void)refreshDataSource {
  61. NSMutableArray *tempArray = [NSMutableArray array];
  62. for (int i = 0; i < self.titles.count; i++) {
  63. JXCategoryTitleVerticalZoomCellModel *cellModel = [[JXCategoryTitleVerticalZoomCellModel alloc] init];
  64. [tempArray addObject:cellModel];
  65. }
  66. self.dataSource = tempArray;
  67. }
  68. - (CGRect)getTargetSelectedCellFrame:(NSInteger)targetIndex selectedType:(JXCategoryCellSelectedType)selectedType
  69. {
  70. CGRect frame = [super getTargetSelectedCellFrame:targetIndex selectedType:selectedType];
  71. if (selectedType == JXCategoryCellSelectedTypeClick ||
  72. selectedType == JXCategoryCellSelectedTypeCode) {
  73. CGFloat contentEdgeInsetScale = self.currentVerticalScale/self.maxVerticalFontScale;
  74. if (targetIndex == 0) {
  75. frame.origin.x += (self.maxVerticalContentEdgeInsetLeft - self.minVerticalContentEdgeInsetLeft)*contentEdgeInsetScale;
  76. }else {
  77. frame.origin.x -= (self.maxVerticalContentEdgeInsetLeft - self.minVerticalContentEdgeInsetLeft)*contentEdgeInsetScale;
  78. }
  79. }
  80. return frame;
  81. }
  82. - (void)refreshCellModel:(JXCategoryBaseCellModel *)cellModel index:(NSInteger)index {
  83. [super refreshCellModel:cellModel index:index];
  84. JXCategoryTitleVerticalZoomCellModel *model = (JXCategoryTitleVerticalZoomCellModel *)cellModel;
  85. model.maxVerticalFontScale = self.maxVerticalFontScale;
  86. }
  87. - (void)refreshLeftCellModel:(JXCategoryBaseCellModel *)leftCellModel rightCellModel:(JXCategoryBaseCellModel *)rightCellModel ratio:(CGFloat)ratio {
  88. [super refreshLeftCellModel:leftCellModel rightCellModel:rightCellModel ratio:ratio];
  89. if (leftCellModel.index == 0) {
  90. //第一个cell正在左右交互
  91. CGFloat scale = self.currentVerticalScale/self.maxVerticalFontScale;
  92. self.contentEdgeInsetLeft = [JXCategoryFactory interpolationFrom:self.maxVerticalContentEdgeInsetLeft*scale to:self.minVerticalContentEdgeInsetLeft*scale percent:ratio];
  93. [self.collectionView.collectionViewLayout invalidateLayout];
  94. [self.collectionView reloadData];
  95. }
  96. }
  97. - (void)refreshSelectedCellModel:(JXCategoryBaseCellModel *)selectedCellModel unselectedCellModel:(JXCategoryBaseCellModel *)unselectedCellModel {
  98. [super refreshSelectedCellModel:selectedCellModel unselectedCellModel:unselectedCellModel];
  99. if (self.isSelectedAnimationEnabled) {
  100. if (selectedCellModel.selectedType == JXCategoryCellSelectedTypeCode ||
  101. selectedCellModel.selectedType == JXCategoryCellSelectedTypeClick) {
  102. self.edgeLeftAnimator = [[JXCategoryViewAnimator alloc] init];
  103. self.edgeLeftAnimator.duration = self.selectedAnimationDuration;
  104. __weak typeof(self) weakSelf = self;
  105. self.edgeLeftAnimator.progressCallback = ^(CGFloat percent) {
  106. selectedCellModel.transitionAnimating = YES;
  107. unselectedCellModel.transitionAnimating = YES;
  108. weakSelf.horizontalZoomTransitionAnimating = YES;
  109. CGFloat scale = weakSelf.currentVerticalScale/weakSelf.maxVerticalFontScale;
  110. if (selectedCellModel.index == 0) {
  111. weakSelf.contentEdgeInsetLeft = [JXCategoryFactory interpolationFrom:weakSelf.minVerticalContentEdgeInsetLeft*scale to:weakSelf.maxVerticalContentEdgeInsetLeft*scale percent:percent];
  112. [weakSelf.collectionView.collectionViewLayout invalidateLayout];
  113. }else if (unselectedCellModel.index == 0) {
  114. weakSelf.contentEdgeInsetLeft = [JXCategoryFactory interpolationFrom:weakSelf.maxVerticalContentEdgeInsetLeft*scale to:weakSelf.minVerticalContentEdgeInsetLeft*scale percent:percent];
  115. [weakSelf.collectionView.collectionViewLayout invalidateLayout];
  116. }
  117. };
  118. self.edgeLeftAnimator.completeCallback = ^{
  119. selectedCellModel.transitionAnimating = NO;
  120. unselectedCellModel.transitionAnimating = NO;
  121. weakSelf.horizontalZoomTransitionAnimating = NO;
  122. };
  123. [self.edgeLeftAnimator start];
  124. }else {
  125. CGFloat scale = self.currentVerticalScale/self.maxVerticalFontScale;
  126. if (selectedCellModel.index == 0) {
  127. self.contentEdgeInsetLeft = self.maxVerticalContentEdgeInsetLeft*scale;
  128. [self.collectionView.collectionViewLayout invalidateLayout];
  129. }else if (unselectedCellModel.index == 0) {
  130. self.contentEdgeInsetLeft = self.minVerticalContentEdgeInsetLeft*scale;
  131. [self.collectionView.collectionViewLayout invalidateLayout];
  132. }
  133. }
  134. }
  135. }
  136. @end