// // KBTabbarController.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBTabbarController.h" #import "KBNavigationController.h" #import "KBHomeMainViewController.h" #import "KBNineNineMainViewController.h" #import "KBBrandMainViewController.h" #import "KBFindMainViewController.h" #import "KBMineMainViewController.h" #import "KBCollectionViewController.h" #import "KBCollectionMainViewController.h" #import "KBClassifyViewController.h" #import "KBCommunityViewController.h" #import "KBCommunityLeftController.h" #import "KBUniteShopCarViewController.h" #import "KBGoosCommunityViewController.h" @interface KBTabbarController () @property (nonatomic,assign) NSInteger indexFlag; @end @implementation KBTabbarController - (void)viewDidLoad { [super viewDidLoad]; [self setUpAllChildVc]; } - (void)setUpAllChildVc { [self setUpOneChildVcWithVc:[[KBHomeMainViewController alloc] init] Image:@"tab_home" selectedImage:@"tab_home_selected" title:@"首页"]; // [self setUpOneChildVcWithVc:[[KBNineNineMainViewController alloc] init] Image:@"tab_99" selectedImage:@"tab_99_selected" title:@"9块9"]; // [self setUpOneChildVcWithVc:[[KBBrandMainViewController alloc] init] Image:@"tab_brand" selectedImage:@"tab_brand_selected" title:@"品牌优选"]; [self setUpOneChildVcWithVc:[[KBGoosCommunityViewController alloc] init] Image:@"tab_better" selectedImage:@"tab_better_sel" title:@"优选"]; [self setUpOneChildVcWithVc:[[KBUniteShopCarViewController alloc] init] Image:@"tab_shopCar" selectedImage:@"tab_shopCar_sel" title:@"购物车"]; [self setUpOneChildVcWithVc:[[KBMineMainViewController alloc] init] Image:@"tab_mine" selectedImage:@"tab_mine_sel" title:@"我的"]; } - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title { [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected]; KBNavigationController *nav = [[KBNavigationController alloc] initWithRootViewController:Vc]; UIImage *myImage = [UIImage imageNamed:image]; myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; Vc.tabBarItem.image = myImage; UIImage *mySelectedImage = [UIImage imageNamed:selectedImage]; mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; Vc.tabBarItem.selectedImage = mySelectedImage; Vc.tabBarItem.title = title; Vc.navigationItem.title = title; [self addChildViewController:nav]; } - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{ NSInteger index = [self.tabBar.items indexOfObject:item]; if (index != self.indexFlag) { //执行动画 NSMutableArray *arry = [NSMutableArray array]; for (UIView *btn in self.tabBar.subviews) { if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) { [arry addObject:btn]; } } //添加动画 //---将下面的代码块直接拷贝到此即可--- self.indexFlag = index; [self clickAnimationWithIndex:index arr:arry]; } } - (void)clickAnimationWithIndex:(NSInteger)index arr:(NSArray *)arry{ //放大效果,并回到原位 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; //速度控制函数,控制动画运行的节奏 animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.duration = 0.2; //执行时间 animation.repeatCount = 1; //执行次数 animation.autoreverses = YES; //完成动画后会回到执行动画之前的状态 animation.fromValue = [NSNumber numberWithFloat:0.7]; //初始伸缩倍数 animation.toValue = [NSNumber numberWithFloat:1.1]; //结束伸缩倍数 [[arry[index] layer] addAnimation:animation forKey:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end