//
// CustomTabBarController.h
// Dream
//
// Created by mac on 14-10-17.
// Copyright (c) 2014年 ninglihuan. All rights reserved.
//
//标记一下方向
typedef enum
{
ZYSlideDirectionRight = 0,
ZYSlideDirectionLeft
} ZYSlideDirection;
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UIViewController<UINavigationControllerDelegate>
{
UIView *_myTabBarView;
}
@property (retain, nonatomic) NSArray *viewControllers;
@property (retain, nonatomic) NSArray *previousNavViewController;
@property (retain, nonatomic) NSArray *buttonArray;
@property (retain, nonatomic) NSArray *normalImageArray;
@property (retain, nonatomic) NSArray *highLightImageArray;
@property (retain, nonatomic) NSArray *nameArray;
@property (assign, nonatomic) int selectedIndex;
- (instancetype)initWithNormalImage:(NSArray *)normalImageArray highLightImage:(NSArray *)highLightImageArray BtnName:(NSArray *)nameArray;
- (void)tabBarButtonClick:(id)sender;
- (void)showTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated;
- (void)hideTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated;
@end
//
// CustomTabBarController.m
// Dream
//
// Created by mac on 14-10-17.
// Copyright (c) 2014年 ninglihuan. All rights reserved.
//
//动画持续时间,该时间与压栈和出栈时间相当
#define SLIDE_ANIMATION_DURATION 0.33
#import "CustomTabBarController.h"
#import "Header.h"
@interface CustomTabBarController ()
@property (retain, nonatomic) NSArray *labArray;
@property (retain, nonatomic) NSArray *imageArray;
@end
@implementation CustomTabBarController
- (void)dealloc
{
[_myTabBarView release];
self.viewControllers = nil;
self.normalImageArray = nil;
self.highLightImageArray = nil;
self.nameArray = nil;
self.previousNavViewController = nil;
self.buttonArray = nil;
self.labArray = nil;
self.imageArray = nil;
[super dealloc];
}
- (instancetype)initWithNormalImage:(NSArray *)normalImageArray highLightImage:(NSArray *)highLightImageArray BtnName:(NSArray *)nameArray
{
if (self = [super init])
{
_selectedIndex = -1;
self.normalImageArray = normalImageArray;
self.highLightImageArray = highLightImageArray;
self.nameArray = nameArray;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
_myTabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 980, 768, 44)];
[self.view addSubview:_myTabBarView];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"adv_bottom.png"]];
image.frame = CGRectMake(0, 0, 768, 2);
[_myTabBarView addSubview:image];
[image release];
NSMutableArray *arrayB = [NSMutableArray array];
NSMutableArray *arrayI = [NSMutableArray array];
NSMutableArray *arrayL = [NSMutableArray array];
for (int i = 0; i < _nameArray.count; i ++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(204+100*i, 2, 60, 40);
btn.tag = i;
// [btn setImage:[UIImage imageNamed:_normalImageArray[i]] forState:UIControlStateNormal];
// [btn setImage:[UIImage imageNamed:_highLightImageArray[i]] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[_myTabBarView addSubview:btn];
[arrayB addObject:btn];
UIImageView *imageView= [[UIImageView alloc]initWithFrame:CGRectMake(15, 3, 30, 20)];
imageView.image = [UIImage imageNamed:_normalImageArray[i]];
imageView.highlightedImage = [UIImage imageNamed:_highLightImageArray[i]];
[btn addSubview:imageView];
[arrayI addObject:imageView];
[imageView release];
UILabel *lab = [[[UILabel alloc]init] autorelease];
lab.text = _nameArray[i];
lab.textAlignment = NSTextAlignmentCenter;
lab.font = [UIFont systemFontOfSize:12];
lab.frame = CGRectMake(0, 25, 60, 10);
[btn addSubview:lab];
[arrayL addObject:lab];
[lab release];
}
self.buttonArray = arrayB;
self.imageArray = arrayI;
self.labArray = arrayL;
if (_selectedIndex == -1)
{
self.selectedIndex = 0;
}
else
{
self.selectedIndex = _selectedIndex;
}
}
- (void)setSelectedIndex:(int)selectedIndex
{
//如果索引值没有改变不做其他操作
if (_selectedIndex == selectedIndex) return;
if (_selectedIndex >= 0)
{
//找出对应索引的视图控制器
UIViewController *priviousViewController = [_viewControllers objectAtIndex:_selectedIndex];
//移除掉
[priviousViewController.view removeFromSuperview];
UIButton *btn = _buttonArray[_selectedIndex];
btn.selected = NO;
UILabel *lab = _labArray[_selectedIndex];
lab.textColor = [UIColor blackColor];
UIImageView *image = _imageArray[_selectedIndex];
image.image = [UIImage imageNamed:_normalImageArray[_selectedIndex]];
}
//记录一下当前的索引
_selectedIndex = selectedIndex;
//获得对应的按钮并且设置为高亮状态下的图片
UIButton *currentButton = _buttonArray[_selectedIndex];
currentButton.selected = YES;
UILabel *lab = _labArray[_selectedIndex];
lab.textColor = PINK(0.6);
UIImageView *image = _imageArray[_selectedIndex];
image.image = [UIImage imageNamed:_highLightImageArray[_selectedIndex]];
//获得对应的视图控制器
UIViewController *currentViewController = [_viewControllers objectAtIndex:_selectedIndex];
//如果此条件成立表示当前是第一个,即“导航控制器”
if ([currentViewController isKindOfClass:[UINavigationController class]])
{
//设置导航控制器的代理
((UINavigationController *)currentViewController).delegate = self;
}
//设置当前视图的大小
currentViewController.view.frame = CGRectMake(0, 0, 768, self.view.bounds.size.height - 43);
//添加到Tab上
[self.view addSubview:currentViewController.view];
//把视图放到TabBar下面
[self.view sendSubviewToBack:currentViewController.view];
}
- (void)tabBarButtonClick:(id)sender
{
//获得索引
UIButton *btn = (UIButton *)sender;
int index = (int)btn.tag;
//用self.赋值默认会调set方法
self.selectedIndex = index;
}
#pragma mark -
#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// NSLog(@"vc is %@",viewController);
/*
第一次加载根视图:previousNavViewController当前导航控制器里面的视图控制器数组
之后显示视图:previousNavViewController操作前的视图控制器数组
*/
if (!_previousNavViewController)
{
//导航控制器中的视图数组
self.previousNavViewController = navigationController.viewControllers;
}
/*
是否为压栈的标记,初始化为NO
如果原来的控制器数不大于当前导航的视图控制器数表示是压栈
*/
BOOL isPush = NO;
if ([_previousNavViewController count] <= [navigationController.viewControllers count])
{
isPush = YES;
}
/*
上一个视图控制器当压栈的时候底部条是否隐藏
当前视图控制器当压栈的时候底部条是否隐藏
这两个视图控制器有可能是同一个
*/
BOOL isPreviousHidden = [[_previousNavViewController lastObject] hidesBottomBarWhenPushed];
BOOL isCurrentHidden = viewController.hidesBottomBarWhenPushed;
// [[navigationController.viewControllers lastObject] hidesBottomBarWhenPushed]
//重新记录当前导航器中的视图控制器数组
self.previousNavViewController = navigationController.viewControllers;
/*
如果状态相同不做其他操作
如果上一个显示NO,这个隐藏YES,则隐藏TabBar
如果上一个隐藏YES,这个显示NO,则显示TabBar
*/
if (!isPreviousHidden && !isCurrentHidden)
{
return;
}
else if(isPreviousHidden && isCurrentHidden)
{
return;
}
else if(!isPreviousHidden && isCurrentHidden)
{
//隐藏tabbar 压栈
[self hideTabBar:isPush ? ZYSlideDirectionLeft : ZYSlideDirectionRight animated:animated];
}
else if(isPreviousHidden && !isCurrentHidden)
{
//显示tabbar 出栈
[self showTabBar:isPush ? ZYSlideDirectionLeft : ZYSlideDirectionRight animated:animated];
}
}
/*
显示底部TabBar相关
需要重置当前视图控制器View的高度为整个屏幕的高度-TabBar的高度
*/
- (void)showTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated
{
//根据压栈还是出栈设置TabBar初始位置
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = self.view.bounds.size.width * ( (direction == ZYSlideDirectionRight) ? -1 : 1);
_myTabBarView.frame = tempRect;
//执行动画
[UIView animateWithDuration:isAnimated ? SLIDE_ANIMATION_DURATION : 0 delay:0 options:0 animations:^
{
//动画效果
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = 0;
_myTabBarView.frame = tempRect;
}
completion:^(BOOL finished)
{
//动画结束时
//重置当前视图控制器View的高度为整个屏幕的高度-TabBar的高度
UIViewController *currentViewController = [_viewControllers objectAtIndex:_selectedIndex];
CGRect viewRect = currentViewController.view.frame;
viewRect.size.height = self.view.bounds.size.height - 43;
currentViewController.view.frame = viewRect;
}];
}
/*
隐藏底部TabBar相关
需要重置当前视图控制器View的高度为整个屏幕的高度
*/
- (void)hideTabBar:(ZYSlideDirection)direction animated:(BOOL)isAnimated
{
//获得当前视图控制器
UIViewController *currentViewController = [_viewControllers objectAtIndex:_selectedIndex];
//重置高度
CGRect viewRect = currentViewController.view.frame;
viewRect.size.height = self.view.bounds.size.height;
currentViewController.view.frame = viewRect;
//设置TabBar的位置
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = 0;
_myTabBarView.frame = tempRect;
//采用Block的形式开启一个动画
[UIView animateWithDuration:isAnimated ? SLIDE_ANIMATION_DURATION : 0 delay:0 options:0 animations:^(void)
{
//根据压栈还是出栈设置动画效果
CGRect tempRect = _myTabBarView.frame;
tempRect.origin.x = self.view.bounds.size.width * (direction == ZYSlideDirectionLeft ? -1 : 1);
_myTabBarView.frame = tempRect;
}
completion:^(BOOL finished){}
];
}
@end