CNLiveTabBarController.m
6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// CNLiveTabBarController.m
// CNLiveScioLive
//
// Created by CNLive-zxw on 2018/9/17.
// Copyright © 2018年 CNLive. All rights reserved.
//
#import "CNLiveTabBarController.h"
#import "XWTabBarModel.h"
@interface CNLiveTabBarController ()<UITabBarDelegate>{
NSInteger _indexFlag;
}
@property (nonatomic, copy) NSMutableArray *controllers;
@end
@implementation CNLiveTabBarController
#pragma mark - life cycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_indexFlag = 0;
[self initItemsAndViewContorllers];
self.tabBar.tintColor = Color_Blue;
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguageVersion:) name:@"changeLanguageVersion" object:nil];
}
//- (void)changeLanguageVersion:(NSNotification *)notification {
//
// for (NSInteger i = 0; i < self.controllers.count; i++) {
// XWTabBarModel *model = self.controllers[i];
// UIViewController *childVC = self.viewControllers[i];
// childVC.tabBarItem.title = IS_CHINESE ? model.title : model.enTitle;
//
// }
//
//}
#pragma mark - private methods
- (void)initItemsAndViewContorllers {
NSMutableArray *viewControllerArr = [NSMutableArray array];
for (XWTabBarModel *model in self.controllers) {
UIViewController *childVC = [NSClassFromString(model.controller) new];
childVC.tabBarItem.title = IS_CHINESE? model.title : model.enTitle;
[childVC.tabBarItem setTitleTextAttributes:@{
NSFontAttributeName : [UIFont systemFontOfSize:10.0],
NSForegroundColorAttributeName : HEXCOLOR(0x555555)}
forState:UIControlStateNormal];
[childVC.tabBarItem setTitleTextAttributes:@{
NSFontAttributeName : [UIFont systemFontOfSize:10.0],
NSForegroundColorAttributeName : Color_Blue}
forState:UIControlStateSelected];
if ([model.image isEqualToString:model.selectedImage]) {
CGSize imageSize = CGSizeMake(54, 54);
UIImage *norMal = [UIImage imageNamed:model.image];
UIImage *image = [self scaleToSize:norMal size:imageSize];
childVC.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVC.tabBarItem.selectedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVC.tabBarItem.imageInsets = UIEdgeInsetsMake(-15, 0, 15, 0);
}else{
childVC.tabBarItem.image = [[UIImage imageNamed:model.image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childVC.tabBarItem.selectedImage = [[UIImage imageNamed:model.selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
CNLiveBaseNavigationController *naviVC = [[CNLiveBaseNavigationController alloc] initWithRootViewController:childVC];
[viewControllerArr addObject:naviVC];
}
self.viewControllers = viewControllerArr;
}
#pragma mark - getters and setters
- (NSMutableArray *)controllers {
if (!_controllers) {
_controllers = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle] pathForResource:@"MainTabbarItem" ofType:@"plist"];
NSArray *modelArr = [NSArray arrayWithContentsOfFile:path];
for (NSInteger i = 0; i < modelArr.count; i++) {
XWTabBarModel *homeModel = [XWTabBarModel mj_objectWithKeyValues:modelArr[i]];
[_controllers addObject:homeModel];
}
}
return _controllers;
}
- (void)dealloc {
// [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLanguageVersion" object:nil];
}
/**
* 改变图片的大小
*
* @param img 需要改变的图片
* @param newsize 新图片的大小
*
* @return 返回修改后的新图片
*/
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(newsize);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
- (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.
}
*/
- (BOOL)shouldAutorotate{
return self.selectedViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.selectedViewController supportedInterfaceOrientations];
}
#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSInteger index = [self.tabBar.items indexOfObject:item];
if (_indexFlag != index) {
[self animationWithIndex:index];
}
}
// 动画
- (void)animationWithIndex:(NSInteger) index {
NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
for (UIView *tabBarButton in self.tabBar.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabbarbuttonArray addObject:tabBarButton];
}
}
CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulse.duration = 0.20;
pulse.repeatCount= 1;
pulse.autoreverses= YES;
pulse.fromValue= [NSNumber numberWithFloat:0.9];
pulse.toValue= [NSNumber numberWithFloat:1.1];
UIView *tabBarButton = tabbarbuttonArray[index];
[[tabBarButton layer] addAnimation:pulse forKey:nil];
_indexFlag = index;
}
@end