// // YHTabbarController.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "YHTabbarController.h" #import "YHNavigationController.h" #import "YHHomeMainViewController.h" #import "YHNineNineMainViewController.h" #import "YHBrandMainViewController.h" #import "YHFindMainViewController.h" #import "YHMineMainViewController.h" #import "YHCollectionViewController.h" @interface YHTabbarController () @property (nonatomic,assign) NSInteger indexFlag; @end @implementation YHTabbarController - (void)viewDidLoad { [super viewDidLoad]; [self setUpAllChildVc]; } - (void)setUpAllChildVc { [self setUpOneChildVcWithVc:[[YHHomeMainViewController alloc] init] Image:@"tab_home" selectedImage:@"tab_home_selected" title:@"首页"]; // [self setUpOneChildVcWithVc:[[YHNineNineMainViewController alloc] init] Image:@"tab_99" selectedImage:@"tab_99_selected" title:@"9块9"]; // [self setUpOneChildVcWithVc:[[YHBrandMainViewController alloc] init] Image:@"tab_brand" selectedImage:@"tab_brand_selected" title:@"品牌优选"]; [self setUpOneChildVcWithVc:[[YHFindMainViewController alloc] init] Image:@"tab_find" selectedImage:@"tab_find_selected" title:@"找券"]; [self setUpOneChildVcWithVc:[[YHCollectionViewController alloc] init] Image:@"tab_collect" selectedImage:@"tab_collect_selected" title:@"收藏"]; [self setUpOneChildVcWithVc:[[YHMineMainViewController alloc] init] Image:@"tab_mine" selectedImage:@"tab_mine_selected" title:@"我的"]; } - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title { [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected]; YHNavigationController *nav = [[YHNavigationController 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