123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // LDTabbarController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/16.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "LDTabbarController.h"
- #import "LDCommunityLeftController.h"
- #import "LDNavigationController.h"
- #import "LDHomeMainViewController.h"
- #import "LDBrandMainViewController.h"
- #import "LDFindMainViewController.h"
- #import "LDMineMainViewController.h"
- #import "LDCollectionViewController.h"
- #import "LDCollectionMainViewController.h"
- #import "LDClassifyViewController.h"
- #import "LDCommunityViewController.h"
- #import "KBUniteShopCarViewController.h"
- #import "LDTimeLineViewController.h"
- #import "LDAdWebViewController.h"
- #import "LDTabbar.h"
- @interface LDTabbarController ()<MCTabBarControllerDelegate>
- {
- LDTabbar *_mcTabbar;
- }
- @property (nonatomic,assign) NSInteger indexFlag;
- @end
- @implementation LDTabbarController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // [self setUpAllChildVc];
-
-
- //选中时的颜色
- // self.mcTabbar.tintColor = [UIColor homeRedColor];
- // //透明设置为NO,显示白色,view的高度到tabbar顶部截止,YES的话到底部
- // self.mcTabbar.translucent = NO;
- //
- // self.mcTabbar.position = MCTabBarCenterButtonPositionBulge;
- // self.mcTabbar.centerImage = [UIImage imageNamed:@"icon_tab_red"];
- // self.mcDelegate = self;
- [self setUpAllChildVc];
-
- }
- - (void)buttonAction:(UIButton *)button{
- self.selectedIndex = 2;//关联中间按钮
-
- //放大效果,并回到原位
- 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]; //结束伸缩倍数
- [button.layer addAnimation:animation forKey:nil];
- }
- //tabbar选择时的代理
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
-
- }
- - (void)setUpAllChildVc
- {
- [self setUpOneChildVcWithVc:[[LDHomeMainViewController alloc] init] Image:@"tab_home_n" selectedImage:@"tab_home_s" title:@"首页"];
- [self setUpOneChildVcWithVc:[[LDTimeLineViewController alloc] init] Image:@"tab_good_n" selectedImage:@"tab_good_s" title:@"优选"];
-
- LDAdWebViewController *web =[[LDAdWebViewController alloc] init];
- web.redStr=@"红包";
- // [self setUpOneChildVcWithVc:web Image:nil selectedImage:nil title:@"红包"];
-
- [self setUpOneChildVcWithVc:web Image:@"tab_car_n" selectedImage:@"tab_car_s" title:@"红包"];
- [self setUpOneChildVcWithVc:[[LDMineMainViewController alloc] init] Image:@"tab_mine_n" selectedImage:@"tab_mine_s" title:@"我的"];
-
- }
- - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
- {
- [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
- LDNavigationController *nav = [[LDNavigationController alloc] initWithRootViewController:Vc];
- [[UITabBar appearance] setTintColor:[UIColor baseColor]];
- if (@available(iOS 10.0, *)) {
- [[UITabBar appearance] setUnselectedItemTintColor:[UIColor lightGrayColor]];
- } else {
- // Fallback on earlier versions
- }
- 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)addChildViewControllerwitVC:(UIViewController*)Vc title:(NSString *)title
- {
- [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
-
- LDNavigationController *nav = [[LDNavigationController alloc] initWithRootViewController:Vc];
- Vc.tabBarItem.title = title;
- Vc.navigationItem.title = title;
- // 选中
- NSMutableDictionary *attrSelected = [NSMutableDictionary dictionary];
- attrSelected[NSForegroundColorAttributeName] = [UIColor baseColor];
-
- [Vc.tabBarItem setTitleTextAttributes:attrSelected forState:UIControlStateNormal];
- [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];
- }
- // 使用MCTabBarController 自定义的 选中代理
- - (void)mcTabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
- if (tabBarController.selectedIndex == 2){
- // [self rotationAnimation];
- }else {
- // [self.mcTabbar.centerBtn.layer removeAllAnimations];
- }
- }
- @end
|