经验demoiOS DeveloperiOS进阶之路

自定义动态TabBar

2017-07-06  本文已影响110人  几分心动i

继承UITabBarController创建YXGTabBarController

#import "YXGTabBarController.h"

@interface YXGTabBarController ()<UITabBarControllerDelegate>
{
    NSInteger _currentIndex;
}
@end

@implementation YXGTabBarController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    UIColor *textColor = [UIColor redColor];
    
    UITabBarItem *item1 = [[UITabBarItem alloc] init];
    item1.tag = 1;
    [item1 setTitle:@"首页1"];
    [item1 setImage:[UIImage imageNamed:@"home1"]];
    [item1 setSelectedImage:[[UIImage imageNamed:@"home1-Click"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item1 setTitleTextAttributes:@{NSForegroundColorAttributeName:textColor} forState:UIControlStateSelected];
    
    UITabBarItem *item2 = [[UITabBarItem alloc] init];
    item2.tag = 2;
    [item2 setTitle:@"首页2"];
    [item2 setImage:[UIImage imageNamed:@"home2"]];
    [item2 setSelectedImage:[[UIImage imageNamed:@"home2-Click"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item2 setTitleTextAttributes:@{NSForegroundColorAttributeName:textColor} forState:UIControlStateSelected];
   
    UITabBarItem *item3 = [[UITabBarItem alloc] init];
    item3.tag = 3;
    [item3 setTitle:@"首页3"];
    [item3 setImage:[UIImage imageNamed:@"home3"]];
    [item3 setSelectedImage:[[UIImage imageNamed:@"home3-Click"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item3 setTitleTextAttributes:@{NSForegroundColorAttributeName:textColor} forState:UIControlStateSelected];
    
    UITabBarItem *item4 = [[UITabBarItem alloc] init];
    item4.tag = 4;
    [item4 setTitle:@"首页4"];
    [item4 setImage:[UIImage imageNamed:@"home4"]];
    [item4 setSelectedImage:[[UIImage imageNamed:@"home4-Click"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [item4 setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}
                         forState:UIControlStateSelected];
    
    
    ViewController *homeController = [[ViewController alloc] init];
    UINavigationController *homeNavController = [[UINavigationController alloc] initWithRootViewController:homeController];
    homeNavController.tabBarItem = item1;
    
    ViewController *investController = [[ViewController alloc] init];
    UINavigationController *projectNavController = [[UINavigationController alloc] initWithRootViewController:investController];
    projectNavController.tabBarItem = item2;
    
    ViewController *loanController = [[ViewController alloc] init];
    UINavigationController *messageNavController = [[UINavigationController alloc] initWithRootViewController:loanController];
    messageNavController.tabBarItem = item3;
    
    ViewController *myController = [[ViewController alloc] init];
    UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myController];
    myNavController.tabBarItem = item4;
    
    self.viewControllers = [NSArray arrayWithObjects:homeNavController,projectNavController,messageNavController,myNavController, nil];
    self.delegate = self;
    self.selectedIndex = 0;
}

#pragma mark - UITabBarController代理方法 点击事件
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    if (tabBarController.selectedIndex == 3) {
        
    }
    //点击tabBarItem动画
    if (self.selectedIndex != _currentIndex)[self tabBarButtonClick:[self getTabBarButton]];
}

- (UIControl *)getTabBarButton{
    
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc] initWithCapacity:0];

    for (UIView *tabBarButton in self.tabBar.subviews) {
        
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButtons addObject:tabBarButton];
        }
    }
    
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
    
    return tabBarButton;
}

- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据需求自定义
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.3;
            animation.calculationMode = kCAAnimationCubic;
            //把动画添加上去就OK了
            [imageView.layer addAnimation:animation forKey:nil];

        }
    }
    _currentIndex = self.selectedIndex;
}
上一篇下一篇

猜你喜欢

热点阅读