猎豆优选

LDTabbarController.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // LDTabbarController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDTabbarController.h"
  9. #import "LDCommunityLeftController.h"
  10. #import "LDNavigationController.h"
  11. #import "LDHomeMainViewController.h"
  12. #import "LDBrandMainViewController.h"
  13. #import "LDFindMainViewController.h"
  14. #import "LDMineMainViewController.h"
  15. #import "LDCollectionViewController.h"
  16. #import "LDCollectionMainViewController.h"
  17. #import "LDClassifyViewController.h"
  18. #import "LDCommunityViewController.h"
  19. #import "KBUniteShopCarViewController.h"
  20. #import "LDTimeLineViewController.h"
  21. #import "LDAdWebViewController.h"
  22. #import "LDTabbar.h"
  23. @interface LDTabbarController ()<MCTabBarControllerDelegate>
  24. {
  25. LDTabbar *_mcTabbar;
  26. }
  27. @property (nonatomic,assign) NSInteger indexFlag;
  28. @end
  29. @implementation LDTabbarController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // [self setUpAllChildVc];
  33. //选中时的颜色
  34. // self.mcTabbar.tintColor = [UIColor homeRedColor];
  35. // //透明设置为NO,显示白色,view的高度到tabbar顶部截止,YES的话到底部
  36. // self.mcTabbar.translucent = NO;
  37. //
  38. // self.mcTabbar.position = MCTabBarCenterButtonPositionBulge;
  39. // self.mcTabbar.centerImage = [UIImage imageNamed:@"icon_tab_red"];
  40. // self.mcDelegate = self;
  41. [self setUpAllChildVc];
  42. }
  43. - (void)buttonAction:(UIButton *)button{
  44. self.selectedIndex = 2;//关联中间按钮
  45. //放大效果,并回到原位
  46. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  47. //速度控制函数,控制动画运行的节奏
  48. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  49. animation.duration = 0.2; //执行时间
  50. animation.repeatCount = 1; //执行次数
  51. animation.autoreverses = YES; //完成动画后会回到执行动画之前的状态
  52. animation.fromValue = [NSNumber numberWithFloat:0.7]; //初始伸缩倍数
  53. animation.toValue = [NSNumber numberWithFloat:1.1]; //结束伸缩倍数
  54. [button.layer addAnimation:animation forKey:nil];
  55. }
  56. //tabbar选择时的代理
  57. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
  58. }
  59. - (void)setUpAllChildVc
  60. {
  61. [self setUpOneChildVcWithVc:[[LDHomeMainViewController alloc] init] Image:@"tab_home_n" selectedImage:@"tab_home_s" title:@"首页"];
  62. [self setUpOneChildVcWithVc:[[LDTimeLineViewController alloc] init] Image:@"tab_good_n" selectedImage:@"tab_good_s" title:@"优选"];
  63. LDAdWebViewController *web =[[LDAdWebViewController alloc] init];
  64. web.redStr=@"红包";
  65. // [self setUpOneChildVcWithVc:web Image:nil selectedImage:nil title:@"红包"];
  66. [self setUpOneChildVcWithVc:web Image:@"tab_car_n" selectedImage:@"tab_car_s" title:@"红包"];
  67. [self setUpOneChildVcWithVc:[[LDMineMainViewController alloc] init] Image:@"tab_mine_n" selectedImage:@"tab_mine_s" title:@"我的"];
  68. }
  69. - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
  70. {
  71. [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
  72. LDNavigationController *nav = [[LDNavigationController alloc] initWithRootViewController:Vc];
  73. [[UITabBar appearance] setTintColor:[UIColor baseColor]];
  74. if (@available(iOS 10.0, *)) {
  75. [[UITabBar appearance] setUnselectedItemTintColor:[UIColor lightGrayColor]];
  76. } else {
  77. // Fallback on earlier versions
  78. }
  79. UIImage *myImage = [UIImage imageNamed:image];
  80. myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  81. Vc.title=title;
  82. Vc.tabBarItem.image = myImage;
  83. UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
  84. mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  85. Vc.tabBarItem.selectedImage = mySelectedImage;
  86. Vc.tabBarItem.title = title;
  87. Vc.navigationItem.title = title;
  88. [self addChildViewController:nav];
  89. }
  90. -(void)addChildViewControllerwitVC:(UIViewController*)Vc title:(NSString *)title
  91. {
  92. [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
  93. LDNavigationController *nav = [[LDNavigationController alloc] initWithRootViewController:Vc];
  94. Vc.tabBarItem.title = title;
  95. Vc.navigationItem.title = title;
  96. // 选中
  97. NSMutableDictionary *attrSelected = [NSMutableDictionary dictionary];
  98. attrSelected[NSForegroundColorAttributeName] = [UIColor baseColor];
  99. [Vc.tabBarItem setTitleTextAttributes:attrSelected forState:UIControlStateNormal];
  100. [self addChildViewController:nav];
  101. }
  102. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
  103. NSInteger index = [self.tabBar.items indexOfObject:item];
  104. if (index != self.indexFlag) {
  105. //执行动画
  106. NSMutableArray *arry = [NSMutableArray array];
  107. for (UIView *btn in self.tabBar.subviews) {
  108. if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  109. [arry addObject:btn];
  110. }
  111. }
  112. self.indexFlag = index;
  113. [self clickAnimationWithIndex:index arr:arry];
  114. }
  115. }
  116. - (void)clickAnimationWithIndex:(NSInteger)index arr:(NSArray *)arry{
  117. //放大效果,并回到原位
  118. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  119. //速度控制函数,控制动画运行的节奏
  120. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  121. animation.duration = 0.2; //执行时间
  122. animation.repeatCount = 1; //执行次数
  123. animation.autoreverses = YES; //完成动画后会回到执行动画之前的状态
  124. animation.fromValue = [NSNumber numberWithFloat:0.7]; //初始伸缩倍数
  125. animation.toValue = [NSNumber numberWithFloat:1.1]; //结束伸缩倍数
  126. [[arry[index] layer] addAnimation:animation forKey:nil];
  127. }
  128. // 使用MCTabBarController 自定义的 选中代理
  129. - (void)mcTabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
  130. if (tabBarController.selectedIndex == 2){
  131. // [self rotationAnimation];
  132. }else {
  133. // [self.mcTabbar.centerBtn.layer removeAllAnimations];
  134. }
  135. }
  136. @end