You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.6 KiB

//
// TFTabbarViewHelper.m
// WXReader
//
// Created by 谢腾飞 on 2020/12/1.
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
//
#import "TFTabbarViewHelper.h"
#import "TFBookRackViewController.h"
#import "TFBookStoreViewController.h"
#import "TFDiscoverViewController.h"
#import "TFMineViewController.h"
@interface TFTabbarViewHelper () <UITabBarControllerDelegate>
@property (nonatomic ,strong ,readwrite) CYLTabBarController *tabBarController;
@end
@implementation TFTabbarViewHelper
- (instancetype)init
{
if (self = [super init]) {
// 隐藏tabbar
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenTabbar:) name:Notification_Hidden_Tabbar object:nil];
// 显示tabbar
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showTabbar:) name:Notification_Show_Tabbar object:nil];
// 改变审核状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeMagicState:) name:Notification_Review_State object:nil];
// 改变tabbar选项卡
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changTabbarIndex:) name:Notification_Change_Tabbar_Index object:nil];
// 切换语言状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchLanguage) name:Notification_Switch_Language object:nil];
}
return self;
}
- (CYLTabBarController *)tabBarController
{
if (!_tabBarController) {
CYLTabBarController *tabBarController = nil;
if (@available(iOS 13.0, *)) {
tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers tabBarItemsAttributes:self.tabBarItemsAttributesForController imageInsets:UIEdgeInsetsMake(1, 0, 1, 0) titlePositionAdjustment:UIOffsetMake(0, -3)];
[[UITabBar appearance] setUnselectedItemTintColor:kColorRGBA(217, 217, 217, 1)];
[[UITabBar appearance] setTintColor:kMainColor];
} else {
tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers tabBarItemsAttributes:self.tabBarItemsAttributesForController imageInsets:UIEdgeInsetsMake(0, 0, 0, 0) titlePositionAdjustment:UIOffsetMake(0, -3)];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor} forState:UIControlStateSelected];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:kColorRGBA(217, 217, 217, 1)} forState:UIControlStateNormal];
}
tabBarController.tabBarHeight = PUB_TABBAR_HEIGHT;
tabBarController.tabBar.translucent = YES;
tabBarController.delegate = self;
tabBarController.tabBar.clipsToBounds = YES;
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:[TFViewHelper scaleImage:[UIImage imageNamed:@"tabbar_background"] toScale:1]];
if (![[NSUserDefaults standardUserDefaults] objectForKey:TF_TABBAR_SELECT_MEMORY]) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:1] forKey:TF_TABBAR_SELECT_MEMORY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSNumber *selectIndex = [[NSUserDefaults standardUserDefaults] objectForKey:TF_TABBAR_SELECT_MEMORY];
tabBarController.selectedIndex = [selectIndex integerValue];
_tabBarController = tabBarController;
}
return _tabBarController;
}
- (NSArray *)tabBarItemsAttributesForController
{
NSMutableArray *tabbarItems = [NSMutableArray array];
for (int i = 0; i < self.titleArr.count; i ++) {
[tabbarItems addObject:@{
CYLTabBarItemTitle : self.titleArr[i],
CYLTabBarItemImage : self.itemImageArr[i],
CYLTabBarItemSelectedImage : self.itemImageSelectedArr[i],
}];
}
NSArray *tabBarItemsAttributes = [tabbarItems copy];
return tabBarItemsAttributes;
}
- (NSArray *)titleArr
{
return @[TFLocalizedString(@"书架"), TFLocalizedString(@"书城"), TFLocalizedString(@"发现"), TFLocalizedString(@"我的")];
}
- (NSArray *)itemImageArr
{
return @[@"bookRack", @"bookMall", @"discover", @"mine"];
}
- (NSArray *)itemImageSelectedArr
{
return @[@"bookRack_select", @"bookMall_select", @"discover_select", @"mine_select"];
}
- (NSArray *)viewControllers
{
NSArray *t_viewControllers;
TFBookRackViewController *bookRackViewController = [[TFBookRackViewController alloc] init];
TFBookStoreViewController *bookMallViewController = [[TFBookStoreViewController alloc] init];
TFDiscoverViewController *discoverViewController = [[TFDiscoverViewController alloc] init];
TFMineViewController *mineViewController = [[TFMineViewController alloc] init];
t_viewControllers = @[bookRackViewController, bookMallViewController, discoverViewController, mineViewController];
NSMutableArray *t_navigationControllers = [NSMutableArray array];
for (UIViewController *t_viewController in t_viewControllers) {
UINavigationController *t_navigationController = [[TFNavigationController alloc] initWithRootViewController:t_viewController];
[t_navigationControllers addObject:t_navigationController];
}
return [t_navigationControllers copy];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectControl:(UIControl *)control
{
[UIApplication sharedApplication].statusBarHidden = NO;
[self saveTabbarSelectedIndex:tabBarController.selectedIndex];
}
- (void)saveTabbarSelectedIndex:(NSUInteger)selectedIndex
{
if (selectedIndex == 0 || selectedIndex == 1) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithUnsignedInteger:selectedIndex] forKey:TF_TABBAR_SELECT_MEMORY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
#pragma mark - Notification
// 隐藏tabbar
- (void)hiddenTabbar:(NSNotification *)notification
{
self.tabBarController.tabBar.hidden = YES;
NSString *animationState = [NSString stringWithFormat:@"%@", notification.object];
if ([animationState isEqualToString:@"1"]) {
self.tabBarController.tabBar.frame = CGRectMake(0, SCREEN_HEIGHT, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
} else {
[UIView animateWithDuration:kAnimatedDuration animations:^{
self.tabBarController.tabBar.frame = CGRectMake(0, SCREEN_HEIGHT, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
}];
}
}
// 显示tabbar
- (void)showTabbar:(NSNotification *)notification
{
NSString *animationState = [NSString stringWithFormat:@"%@", notification.object];
self.tabBarController.tabBar.hidden = NO;
if ([animationState isEqualToString:@"animation"]) {
[UIView animateWithDuration:kAnimatedDuration animations:^{
self.tabBarController.tabBar.frame = CGRectMake(0, SCREEN_HEIGHT - self.tabBarController.tabBar.frame.size.height, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
}];
} else {
self.tabBarController.tabBar.frame = CGRectMake(0, SCREEN_HEIGHT - self.tabBarController.tabBar.frame.size.height, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
}
}
// 改变tabbar选项卡位置
- (void)changTabbarIndex:(NSNotification *)notification
{
[self.tabBarController setSelectedIndex:[notification.object integerValue]];
NSUInteger selectIndex = [notification.object integerValue];
[self saveTabbarSelectedIndex:selectIndex];
}
// 改变tabbar结构
- (void)changeMagicState:(NSNotification *)notification
{
// 如果本地状态与通知状态相同,就不更改界面
if ([TFSystemInfoManager.magicStatus isEqualToString:notification.object]) {
return;
}
TFSystemInfoManager.magicStatus = notification.object;
self.tabBarController = nil;
[kMainWindow setRootViewController:self.tabBarController];
}
- (void)switchLanguage
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(switchLanguage) withObject:nil waitUntilDone:YES];
return;
}
self.tabBarController = nil;
kMainWindow.rootViewController = self.tabBarController;
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Change_Tabbar_Index object:@"1"];
}
@end