// // ZBTabController.m // ZBProject // // Created by 学丽 on 2019/3/26. // Copyright © 2019 ZB. All rights reserved. // #import "ZBTabController.h" #import "ZBNavigationController.h" @interface ZBTabController () @property (nonatomic,assign) NSInteger indexFlag; @end @implementation ZBTabController - (void)viewDidLoad { [super viewDidLoad]; [self setUpOneChildVcWithVc:[[OrderViewController alloc] init] Image:@"tab_order_n" selectedImage:@"tab_order_s" title:@"首页"]; [self setUpOneChildVcWithVc:[[ProductViewController alloc] init] Image:@"tab_data_n" selectedImage:@"tab_data_s" title:@"选品"]; [self setUpOneChildVcWithVc:[[DataViewController alloc] init] Image:@"tab_sy_n" selectedImage:@"tab_sy_s" title:@"收益"]; // [self setUpOneChildVcWithVc:[[MineViewController alloc] init] Image:@"tab_home_n" selectedImage:@"tab_home_s" title:@"我的"]; } - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title { [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected]; ZBNavigationController *nav = [[ZBNavigationController alloc] initWithRootViewController:Vc]; UIImage *myImage = [UIImage imageNamed:image]; myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; Vc.title=title; 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]; } @end