口袋优选

KBTabbarController.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // KBTabbarController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBTabbarController.h"
  9. #import "KBNavigationController.h"
  10. #import "KBHomeMainViewController.h"
  11. #import "KBNineNineMainViewController.h"
  12. #import "KBBrandMainViewController.h"
  13. #import "KBFindMainViewController.h"
  14. #import "KBMineMainViewController.h"
  15. #import "KBCollectionViewController.h"
  16. #import "KBCollectionMainViewController.h"
  17. #import "KBClassifyViewController.h"
  18. #import "KBCommunityViewController.h"
  19. #import "KBCommunityLeftController.h"
  20. #import "KBUniteShopCarViewController.h"
  21. #import "KBGoosCommunityViewController.h"
  22. @interface KBTabbarController ()<UITabBarControllerDelegate>
  23. @property (nonatomic,assign) NSInteger indexFlag;
  24. @end
  25. @implementation KBTabbarController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. [self setUpAllChildVc];
  29. }
  30. - (void)setUpAllChildVc
  31. {
  32. [self setUpOneChildVcWithVc:[[KBHomeMainViewController alloc] init] Image:@"tab_home" selectedImage:@"tab_home_selected" title:@"首页"];
  33. // [self setUpOneChildVcWithVc:[[KBNineNineMainViewController alloc] init] Image:@"tab_99" selectedImage:@"tab_99_selected" title:@"9块9"];
  34. // [self setUpOneChildVcWithVc:[[KBBrandMainViewController alloc] init] Image:@"tab_brand" selectedImage:@"tab_brand_selected" title:@"品牌优选"];
  35. [self setUpOneChildVcWithVc:[[KBGoosCommunityViewController alloc] init] Image:@"tab_better" selectedImage:@"tab_better_sel" title:@"优选"];
  36. [self setUpOneChildVcWithVc:[[KBUniteShopCarViewController alloc] init] Image:@"tab_shopCar" selectedImage:@"tab_shopCar_sel" title:@"购物车"];
  37. [self setUpOneChildVcWithVc:[[KBMineMainViewController alloc] init] Image:@"tab_mine" selectedImage:@"tab_mine_sel" title:@"我的"];
  38. }
  39. - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
  40. {
  41. [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
  42. KBNavigationController *nav = [[KBNavigationController alloc] initWithRootViewController:Vc];
  43. UIImage *myImage = [UIImage imageNamed:image];
  44. myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  45. Vc.tabBarItem.image = myImage;
  46. UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
  47. mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  48. Vc.tabBarItem.selectedImage = mySelectedImage;
  49. Vc.tabBarItem.title = title;
  50. Vc.navigationItem.title = title;
  51. [self addChildViewController:nav];
  52. }
  53. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
  54. NSInteger index = [self.tabBar.items indexOfObject:item];
  55. if (index != self.indexFlag) {
  56. //执行动画
  57. NSMutableArray *arry = [NSMutableArray array];
  58. for (UIView *btn in self.tabBar.subviews) {
  59. if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  60. [arry addObject:btn];
  61. }
  62. }
  63. //添加动画
  64. //---将下面的代码块直接拷贝到此即可---
  65. self.indexFlag = index;
  66. [self clickAnimationWithIndex:index arr:arry];
  67. }
  68. }
  69. - (void)clickAnimationWithIndex:(NSInteger)index arr:(NSArray *)arry{
  70. //放大效果,并回到原位
  71. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  72. //速度控制函数,控制动画运行的节奏
  73. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  74. animation.duration = 0.2; //执行时间
  75. animation.repeatCount = 1; //执行次数
  76. animation.autoreverses = YES; //完成动画后会回到执行动画之前的状态
  77. animation.fromValue = [NSNumber numberWithFloat:0.7]; //初始伸缩倍数
  78. animation.toValue = [NSNumber numberWithFloat:1.1]; //结束伸缩倍数
  79. [[arry[index] layer] addAnimation:animation forKey:nil];
  80. }
  81. - (void)didReceiveMemoryWarning {
  82. [super didReceiveMemoryWarning];
  83. // Dispose of any resources that can be recreated.
  84. }
  85. /*
  86. #pragma mark - Navigation
  87. // In a storyboard-based application, you will often want to do a little preparation before navigation
  88. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  89. // Get the new view controller using [segue destinationViewController].
  90. // Pass the selected object to the new view controller.
  91. }
  92. */
  93. @end