@[TOC](iOS开发-Tabbar三种点击状态(包括index = 0的tabbar渐变透明) 一)
前言
- 在现在有很多直播的app很新颖,会有很多的底部渐变的效果。
如图
- 说明:暂时没有效果图,只是当作笔记,可以翻看另外一篇博客Tabbar自定义查看
上代码
BaseTabbarController
- BaseTabbarController.h
#import <UIKit/UIKit.h>
@interface BaseTabbarController : UITabBarController
@property(nonatomic, copy) void(^tabbarSelectIndex)(NSInteger index);
- (void)setTabbarControllerColor:(UIColor *)backColor;
@end
- BaseTabbarController.m
#import "BaseTabbarController.h"
@interface BaseTabbarController () <UITabBarControllerDelegate>
@end
@implementation BaseTabbarController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
if(kInitIndex == 0) {
self.tabBar.backgroundImage = [self bm_colorChangeWithSize:CGSizeMake(SCREEN_WIDTH, TABBAR_HEIGHT) startColor:[UIColor colorWithWhite:0 alpha:0] endColor:[UIColor colorWithWhite:0 alpha:1]];
} else {
self.tabBar.backgroundImage = [self imageWithColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.4]];
}
self.tabBar.shadowImage = [UIImage new];
}
- (void)setTabbarControllerColor:(UIColor *)backColor {
self.tabBar.barTintColor = backColor;
}
#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if(tabBarController.selectedIndex == 0) {
self.tabBar.backgroundImage = [self bm_colorChangeWithSize:CGSizeMake(SCREEN_WIDTH, TABBAR_HEIGHT) startColor:[UIColor colorWithWhite:0 alpha:0] endColor:[UIColor colorWithWhite:0 alpha:1]];//[self imageWithColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
self.tabBar.shadowImage = [UIImage new];
} else {
self.tabBar.backgroundImage = [self imageWithColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.4]];
self.tabBar.shadowImage = [UIImage new];
}
if(self.tabbarSelectIndex) {
self.tabbarSelectIndex(tabBarController.selectedIndex);
}
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)bm_colorChangeWithSize:(CGSize)size startColor:(UIColor *)startcolor endColor:(UIColor *)endColor { //底部渐变处理
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(0,0, size.width, size.height);
CGPoint startPoint = CGPointZero;
//startPoint = CGPointMake(0.0,0.0);
gradientLayer.startPoint = startPoint;
CGPoint endPoint = CGPointZero;
endPoint = CGPointMake(0.0,1.0);
gradientLayer.endPoint = endPoint;
gradientLayer.colors = @[(__bridge id)startcolor.CGColor, (__bridge id)endColor.CGColor];
UIGraphicsBeginImageContext(size);
[gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
LoginViewController(这是当我点击登录后生成的rootViewController和四个ViewController)
+ (void)createTabbarAnimation:(BOOL)isAnimated {
// Match
MatchViewController *matchViewController = [MatchViewController new];
matchViewController.tabBarItem.title = @"Match";
matchViewController.tabBarItem.image = [[UIImage imageNamed:@"Match_normal_icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
matchViewController.tabBarItem.selectedImage = [[UIImage imageNamed:@"Match_select_icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[matchViewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HN_Color_Hex(#C4D2E6)} forState:UIControlStateNormal];
[matchViewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HN_Color_Hex(#006CFF)} forState:UIControlStateSelected];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:matchViewController];
nav1.navigationBar.hidden = YES;
//History
HistoryViewController *historyViewController = [HistoryViewController new];
historyViewController.tabBarItem.title = @"History";
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:historyViewController];
nav2.navigationBar.hidden = YES;
//Message
MessageListViewController *messageViewController = [MessageListViewController new];
messageViewController.tabBarItem.title = @"Message";
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:messageViewController];
nav3.navigationBar.hidden = YES;
//Me
MeViewController *meViewController = [MeViewController new];
meViewController.tabBarItem.title = @"Me";
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:meViewController];
nav4.navigationBar.hidden = YES;
NSArray<UIViewController *> *vcsArr = [NSArray<UIViewController *> arrayWithObjects:historyViewController, messageViewController, meViewController, nil];
NSArray<NSString *> *normalWhiteImageNames = [NSArray<NSString *> arrayWithObjects:@"History_normal_white_icon", @"Message_normal_white_icon", @"Me_normal_white_icon", nil];
NSArray<NSString *> *normalImageNames = [NSArray<NSString *> arrayWithObjects:@"History_normal_icon", @"Message_normal_icon", @"Me_normal_icon", nil];
NSArray<NSString *> *selectImageNames = [NSArray<NSString *> arrayWithObjects:@"History_select_icon", @"Message_select_icon", @"Me_select_icon", nil];
if(kInitIndex == 0) {
//Normal
[self __changeViewController:vcsArr forState:UIControlStateNormal color:[UIColor whiteColor]];
[self __changeViewController:vcsArr forState:UIControlStateNormal imagesName:normalWhiteImageNames];
//Select
[self __changeViewController:vcsArr forState:UIControlStateSelected color:HN_Color_Hex(#006CFF)];
} else {
//Normal
[self __changeViewController:vcsArr forState:UIControlStateNormal color:HN_Color_Hex(#C4D2E6)];
[self __changeViewController:vcsArr forState:UIControlStateNormal imagesName:normalImageNames];
//Select
[self __changeViewController:vcsArr forState:UIControlStateSelected color:HN_Color_Hex(#006CFF)];
[self __changeViewController:vcsArr forState:UIControlStateSelected imagesName:selectImageNames];
}
BaseTabbarController *tabbar = [[BaseTabbarController alloc] init];
tabbar.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
tabbar.selectedIndex = kInitIndex;
[tabbar setTabbarSelectIndex:^(NSInteger index) {
if(index == 0) {
//Normal
[self __changeViewController:vcsArr forState:UIControlStateNormal color:[UIColor whiteColor]];
[self __changeViewController:vcsArr forState:UIControlStateNormal imagesName:normalWhiteImageNames];
} else {
//Normal
[self __changeViewController:vcsArr forState:UIControlStateNormal color:HN_Color_Hex(#C4D2E6)];
[self __changeViewController:vcsArr forState:UIControlStateNormal imagesName:normalImageNames];
//Select
[self __changeViewController:vcsArr forState:UIControlStateSelected color:HN_Color_Hex(#006CFF)];
[self __changeViewController:vcsArr forState:UIControlStateSelected imagesName:selectImageNames];
}
}];
[[BaseNavigationManager shareInstance].NavigationController pushViewController:tabbar animated:isAnimated];//[BaseNavigationManager shareInstance].NavigationController 是我封装的一个UINavigationController,大家可以看情况使用
}
+ (void)__changeViewController:(NSArray<UIViewController *> *)viewControllers
forState:(UIControlState)state
color:(UIColor *)color { //改变title颜色
for (UIViewController *vc in viewControllers) {
[vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:color} forState:state];
}
}
+ (void)__changeViewController:(NSArray<UIViewController *> *)viewControllers
forState:(UIControlState)state
imagesName:(NSArray<NSString *> *)imagesName { //改变image颜色
for (int i = 0; i < viewControllers.count; i ++) {
if(state == UIControlStateNormal) {
viewControllers[i].tabBarItem.image = [[UIImage imageNamed:imagesName[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
if(state == UIControlStateSelected) {
viewControllers[i].tabBarItem.selectedImage = [[UIImage imageNamed:imagesName[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}
}