TYTabButtonPagerController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #import "TYTabButtonPagerController.h"
  2. @interface TYTabButtonPagerController ()
  3. @property (nonatomic, assign) CGFloat selectFontScale;
  4. @end
  5. #define kUnderLineViewHeight 3
  6. #define kTitleColor RGB(254, 0, 57)
  7. #define ess_color [UIColor colorWithRed:139/255.0 green:78/255.0 blue:252/255.0 alpha:1]
  8. @implementation TYTabButtonPagerController
  9. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  10. {
  11. if (self = [super initWithCoder:aDecoder]) {
  12. [self configureTabButtonPropertys];
  13. }
  14. return self;
  15. }
  16. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  17. {
  18. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  19. [self configureTabButtonPropertys];
  20. }
  21. return self;
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. if (!self.delegate) {
  27. self.delegate = self;
  28. }
  29. if (!self.dataSource) {
  30. self.dataSource = self;
  31. }
  32. _selectFontScale = self.normalTextFont.pointSize/self.selectedTextFont.pointSize;
  33. [self configureSubViews];
  34. }
  35. - (void)configureSubViews
  36. {
  37. //progress
  38. //下划线颜色
  39. self.progressView.backgroundColor = ess_color;
  40. //self.progressView.layer.cornerRadius = 20;
  41. //self.progressView.layer.masksToBounds = YES;
  42. // tabBar
  43. self.pagerBarView.backgroundColor = _pagerBarColor;
  44. self.collectionViewBar.backgroundColor = _collectionViewBarColor;
  45. }
  46. - (void)configureTabButtonPropertys
  47. {
  48. self.cellSpacing = 2;
  49. self.cellEdging = 3;
  50. self.barStyle = TYPagerBarStyleProgressView;
  51. _normalTextColor = kBlackColor;
  52. // UIColorFromRGB(0x999999);//home 选中颜色
  53. _selectedTextColor = [UIColor colorWithHexString:@"#9152F8"];
  54. // UIColorFromRGB(0x7e37fb);
  55. _selectedbackcolor = kRedColor;
  56. // UIColorFromRGB(0x999999);
  57. _normalbackcolor = kGreenColor;
  58. // [UIColor colorWithRed:241/255.0 green:242/255.0 blue:245/255.0 alpha:1.0];
  59. _pagerBarColor = kClearColor;
  60. _collectionViewBarColor = [UIColor clearColor];
  61. //_progressRadius = self.progressHeight/2;
  62. [self registerCellClass:[TYTabTitleViewCell class] isContainXib:NO];
  63. }
  64. - (void)setBarStyle:(TYPagerBarStyle)barStyle
  65. {
  66. [super setBarStyle:barStyle];
  67. switch (barStyle) {
  68. case TYPagerBarStyleProgressView:
  69. self.progressWidth = 0;
  70. self.progressHeight = kUnderLineViewHeight;
  71. self.progressEdging = 3;
  72. break;
  73. case TYPagerBarStyleProgressBounceView:
  74. self.progressHeight = kUnderLineViewHeight;
  75. self.progressWidth = 20;
  76. break;
  77. case TYPagerBarStyleCoverView:
  78. self.progressWidth = 3;
  79. self.progressHeight = self.contentTopEdging-8;
  80. self.progressEdging = -self.progressHeight/4;
  81. break;
  82. default:
  83. break;
  84. }
  85. if (barStyle == TYPagerBarStyleCoverView) {
  86. self.progressColor = [UIColor lightGrayColor];
  87. }else {
  88. self.progressColor = UIColorFromRGB(0x7e37fb);
  89. }
  90. self.progressRadius = self.progressHeight/2;
  91. }
  92. #pragma mark - private
  93. - (void)transitionFromCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)fromCell toCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)toCell
  94. {
  95. if (fromCell) {
  96. fromCell.titleLabel.textColor = _normalTextColor;
  97. // UIColorFromRGB(0x999999);
  98. // fromCell.titleLabel.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:245/255.0 alpha:1.0];
  99. fromCell.transform = CGAffineTransformMakeScale(self.selectFontScale, self.selectFontScale);
  100. }
  101. if (toCell) {
  102. toCell.titleLabel.textColor = _selectedTextColor;
  103. // Main_textColor;
  104. // toCell.titleLabel.backgroundColor = [UIColor blueColor];
  105. toCell.transform = CGAffineTransformIdentity;
  106. }
  107. }
  108. - (void)transitionFromCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)fromCell toCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)toCell progress:(CGFloat)progress
  109. {
  110. CGFloat currentTransform = (1.0 - self.selectFontScale)*progress;
  111. fromCell.transform = CGAffineTransformMakeScale(1.0-currentTransform, 1.0-currentTransform);
  112. toCell.transform = CGAffineTransformMakeScale(self.selectFontScale+currentTransform, self.selectFontScale+currentTransform);
  113. CGFloat narR,narG,narB,narA;
  114. CGFloat selR,selG,selB,selA;
  115. [self.normalTextColor getRed:&narR green:&narG blue:&narB alpha:&narA];
  116. [self.selectedTextColor getRed:&selR green:&selG blue:&selB alpha:&selA];
  117. CGFloat detalR = narR - selR ,detalG = narG - selG,detalB = narB - selB,detalA = narA - selA;
  118. fromCell.titleLabel.textColor = _normalTextColor;
  119. // [UIColor colorWithRed:selR+detalR*progress green:selG+detalG*progress blue:selB+detalB*progress alpha:selA+detalA*progress];
  120. toCell.titleLabel.textColor = _selectedTextColor;
  121. // [UIColor colorWithRed:narR-detalR*progress green:narG-detalG*progress blue:narB-detalB*progress alpha:narA-detalA*progress];
  122. }
  123. #pragma mark - TYPagerControllerDataSource
  124. - (NSInteger)numberOfControllersInPagerController
  125. {
  126. NSAssert(NO, @"you must impletement method numberOfControllersInPagerController");
  127. return 1;
  128. }
  129. - (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
  130. {
  131. NSAssert(NO, @"you must impletement method pagerController:controllerForIndex:");
  132. return nil;
  133. }
  134. #pragma mark - TYTabPagerControllerDelegate
  135. - (void)pagerController:(TYTabPagerController *)pagerController configreCell:(TYTabTitleViewCell *)cell forItemTitle:(NSString *)title atIndexPath:(NSIndexPath *)indexPath
  136. {
  137. TYTabTitleViewCell *titleCell = (TYTabTitleViewCell *)cell;
  138. titleCell.titleLabel.text = title;
  139. titleCell.titleLabel.font = self.selectedTextFont;
  140. }
  141. - (void)pagerController:(TYTabPagerController *)pagerController transitionFromeCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)fromCell toCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)toCell animated:(BOOL)animated
  142. {
  143. if (animated) {
  144. [UIView animateWithDuration:self.animateDuration animations:^{
  145. [self transitionFromCell:(TYTabTitleViewCell *)fromCell toCell:(TYTabTitleViewCell *)toCell];
  146. }];
  147. }else{
  148. [self transitionFromCell:fromCell toCell:toCell];
  149. }
  150. }
  151. - (void)pagerController:(TYTabPagerController *)pagerController transitionFromeCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)fromCell toCell:(UICollectionViewCell<TYTabTitleCellProtocol> *)toCell progress:(CGFloat)progress
  152. {
  153. [self transitionFromCell:fromCell toCell:toCell progress:progress];
  154. }
  155. @end