// // HCTabbarController.m // hc // // Created by hc on 2018/1/16. // Copyright © 2018年 hc. All rights reserved. // #import "HCTabbarController.h" #import "HCNavigationController.h" #import "HCNineNineMainViewController.h" #import "HCBrandMainViewController.h" #import "HCFindMainViewController.h" #import "HCMineMainViewController.h" #import "HCCollectionViewController.h" #import "HCCollectionMainViewController.h" #import "HCClassifyViewController.h" #import "HCCommunityViewController.h" #import "HCCommunityLeftController.h" #import "HCUniteShopCarViewController.h" #import "HCGoosCommunityViewController.h" #import "FSBaseViewController.h" @interface HCTabbarController () @property (nonatomic,assign) NSInteger indexFlag; @end @implementation HCTabbarController - (void)viewDidLoad { [super viewDidLoad]; [self setUpAllChildVc]; } - (void)setUpAllChildVc { [self setUpOneChildVcWithVc:[[HCTopNewViewController alloc] init] Image:@"tab_home" selectedImage:@"tab_home_selected" title:@"首页"]; // [self setUpOneChildVcWithVc:[[HCNineNineMainViewController alloc] init] Image:@"tab_99" selectedImage:@"tab_99_selected" title:@"9块9"]; // [self setUpOneChildVcWithVc:[[HCBrandMainViewController alloc] init] Image:@"tab_brand" selectedImage:@"tab_brand_selected" title:@"品牌优选"]; [self setUpOneChildVcWithVc:[[FSBaseViewController alloc] init] Image:@"tab_better" selectedImage:@"tab_better_sel" title:@"超级大牌"]; [self setUpOneChildVcWithVc:[[HCUniteShopCarViewController alloc] init] Image:@"tab_shopCar" selectedImage:@"tab_shopCar_sel" title:@"购物车"]; [self setUpOneChildVcWithVc:[[HCMineMainViewController 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]; HCNavigationController *nav = [[HCNavigationController 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