//
// MainTabBarController.m
// UITabBarControllerCustomDemo
//
// Created by LiZe on 13-8-29.
// Copyright (c) 2013年 BlackCode. All rights reserved.
//
#import "MainTabBarController.h"
@interface MainTabBarController () {
float imageHeightY; // 所有图片距离y轴的位置
UILabel *happyLabel; // 开心标签
UILabel *quietLabel; // 安静
UILabel *sadLabel; // 伤心
UILabel *exctingLabel; // 兴奋
UILabel *missLabel; // 想念
}
#pragma mark - 声明--加载全部UIViewController的方法
- (void)_loadViewAllController;
#pragma mark - 声明--加载CustomUITabBarController的方法
- (void)_loadViewCustomUITabBarController;
@end
@implementation MainTabBarController
#pragma mark - 实现----加载全部UIViewController的方法
- (void)_loadViewAllController {
// 添加日记视图
HappyViewController *happyVC = [[HappyViewController alloc] init];
QuietViewController *quietVC = [[QuietViewController alloc] init];
SadViewController *sadVC = [[SadViewController alloc] init];
ExctingViewController *exctingVC = [[ExctingViewController alloc] init];
MissViewController *missVC = [[MissViewController alloc] init];
// 放入到数组中
NSArray *viewController = @[happyVC, quietVC, sadVC, exctingVC,missVC];
[happyVC release], happyVC = nil;
[quietVC release], quietVC = nil;
[sadVC release], sadVC = nil;
[exctingVC release], exctingVC = nil;
[missVC release], missVC = nil;
// 添加到当前View的TabBarController上面
[self setViewControllers:viewController animated:YES];
}
#pragma mark - 实现----加载CustomUITabBarController的方法
- (void)_loadViewCustomUITabBarController {
// 所有图片距离y轴的位置
imageHeightY = self.view.frame.size.height - 60;
NSLog(@"%f", imageHeightY);
// 设置背景图片
_tabBarBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,imageHeightY, 320, 60)];
_tabBarBackground.image = [UIImage imageNamed:@"tab_bar_background"];
_tabBarBackground.userInteractionEnabled = YES; // 一定要设置为可以与用户交互
[self.view addSubview:_tabBarBackground];
// 设置上面的UITabBarItem
// 开心
UIButton *happyButton = [UIButton buttonWithType:UIButtonTypeCustom];
happyButton.frame = CGRectMake(-18, -10, 108, 76);
happyButton.tag = 0;
[_tabBarBackground addSubview:happyButton];
// 安静
UIButton *quietButton = [UIButton buttonWithType:UIButtonTypeCustom];
quietButton.frame = CGRectMake(44, -10, 108, 76);
quietButton.tag = 1;
[_tabBarBackground addSubview:quietButton];
// 伤心
UIButton *sadEassyButton = [UIButton buttonWithType:UIButtonTypeCustom];
sadEassyButton.frame = CGRectMake(105, -10, 108, 76);
sadEassyButton.tag = 2;
[_tabBarBackground addSubview:sadEassyButton];
// 兴奋
UIButton *exctingEassyButton = [UIButton buttonWithType:UIButtonTypeCustom];
exctingEassyButton.frame = CGRectMake(166, -10, 108, 76);
exctingEassyButton.tag = 3;
[_tabBarBackground addSubview:exctingEassyButton];
// 想念
UIButton *missEassyButton = [UIButton buttonWithType:UIButtonTypeCustom];
missEassyButton.frame = CGRectMake(227, -10, 108, 76);
missEassyButton.tag = 4;
[_tabBarBackground addSubview:missEassyButton];
// 快速遍历设置UIButton
for (UIButton *button in _tabBarBackground.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
[button setImage:[UIImage imageNamed:@"tab_bar_normal"]forState:UIControlStateNormal];
[button addTarget:self action:@selector(tabBarItemChangeSelected:)forControlEvents:UIControlEventTouchUpInside]; // 添加监听器
}
}
// 设置被选中的图片
_tabBarSelectedImage = [[UIImageView alloc] initWithFrame:CGRectMake(-18, -10, 108, 76)];
_tabBarSelectedImage.image = [UIImage imageNamed:@"tab_bar_select"];
[_tabBarBackground addSubview:_tabBarSelectedImage]; // 添加到当前的背景上
// 开心标签
happyLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 14, 50, 30)];
happyLabel.text = @"开心";
[_tabBarBackground insertSubview:happyLabelaboveSubview:_tabBarSelectedImage];
[happyLabel release], happyLabel = nil;
// 安静
quietLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 13, 50, 30)];
quietLabel.text = @"安静";
[_tabBarBackground addSubview:quietLabel];
[quietLabel release], quietLabel = nil;
// 伤心
sadLabel = [[UILabel alloc] initWithFrame:CGRectMake(143, 14, 50, 30)];
sadLabel.text = @"伤心";
[_tabBarBackground addSubview:sadLabel];
[sadLabel release], sadLabel = nil;
// 兴奋
exctingLabel = [[UILabel alloc] initWithFrame:CGRectMake(203, 14, 50, 30)];
exctingLabel.text = @"兴奋";
[_tabBarBackground addSubview:exctingLabel];
[exctingLabel release], exctingLabel = nil;
// 想念
missLabel = [[UILabel alloc] initWithFrame:CGRectMake(263, 14, 50, 30)];
missLabel.text = @"想念";
[_tabBarBackground addSubview:missLabel];
[missLabel release], missLabel = nil;
// 快速遍历设置UILabel
for (UILabel *label in _tabBarBackground.subviews) {
if ([label isKindOfClass:[UILabel class]]) {
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor brownColor];
label.font = [UIFont boldSystemFontOfSize:18.0f];
}
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
// 隐藏TabBarController
self.tabBar.hidden = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 调用方法,加载全部页面
[self _loadViewAllController];
// 调用方法,自定义TabBarController
[self _loadViewCustomUITabBarController];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 实现----tabBarItem选中的方法
- (void)tabBarItemChangeSelected:(UIButton *) button {
self.selectedIndex = button.tag;
// 设置动画效果
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
switch (button.tag) {
case 0:
_tabBarSelectedImage.frame = CGRectMake(-18, -10, 108, 76);
break;
case 1:
_tabBarSelectedImage.frame = CGRectMake(44, -10, 108, 76);
break;
case 2:
_tabBarSelectedImage.frame = CGRectMake(105, -10, 108, 76);
break;
case 3:
_tabBarSelectedImage.frame = CGRectMake(166, -10, 108, 76);
break;
case 4:
_tabBarSelectedImage.frame = CGRectMake(227, -10, 108, 76);
break;
default:
break;
}
[UIView commitAnimations];
}
#pragma mark - 重写dealloc方法
-(void)dealloc {
[_tabBarBackground release], _tabBarBackground = nil;
[_tabBarSelectedImage release], _tabBarSelectedImage = nil;
[super dealloc];
}
@end