123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // KSTabBarViewController.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/17.
- //
- #import "KSTabBarViewController.h"
- #import "CustomNavViewController.h"
- #import "KSBaseViewController.h"
- #import <UIImage+Color.h>
- #import "AnimationHelper.h"
- #import "MineWorksViewController.h"
- #import "KSBaseWKWebViewController.h"
- @interface KSTabBarViewController ()<UITabBarControllerDelegate>
- @end
- @implementation KSTabBarViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.delegate = self;
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDraftPage) name:@"DisplayDraft" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showWorksPage:) name:@"displayWorks" object:nil];
- [self configItems];
- }
- - (void)showWorksPage:(NSNotification *)notification {
- [self showWorksDraft:NO info:notification.object];
- }
- - (void)showWorksDraft:(BOOL)isDraft info:(NSDictionary *)info {
- [self tabBarSelectedWithIndex:4];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- CustomNavViewController *navCtrl = self.selectedViewController;
- MineWorksViewController *ctrl = [[MineWorksViewController alloc] init];
- [ctrl displayDraft:isDraft];
- [navCtrl pushViewController:ctrl animated:YES];
- if (isDraft == NO) {
- // 显示作品详情
- NSString *url = [info ks_stringValueForKey:@"url"];
- if (![NSString isEmptyString:url]) {
- KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
- ctrl.url = url;
- [navCtrl pushViewController:ctrl animated:YES];
- }
- }
- });
- }
- - (void)showDraftPage {
- [self showWorksDraft:YES info:nil];
- }
- - (void)configItems {
- // 处理 pad上 tabbar样式
- if (@available(iOS 18.0, *)) {
- if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
- {
- self.traitOverrides.horizontalSizeClass = UIUserInterfaceSizeClassCompact; //TAB BAR ON BOTTOM
- }
- }
- NSArray *controllerArray = @[@"HomeViewController",@"CourseViewController",@"ChatViewController",@"ShopMallViewController",@"MineViewController"];
- NSArray *titleArray = @[@"首页",@"课表",@"聊天",@"商城",@"我的"];
- NSArray *imageArray = @[@"tab_home_unselected",@"tab_course_unselected",@"tab_chat_unselected",@"tab_mall_unselected",@"tab_mine_unselected"];
- NSArray *selectedImgArray = @[@"tab_home_selected",@"tab_course_selected",@"tab_chat_selected",@"tab_mall_selected",@"tab_mine_selected"];
- NSMutableArray *ctrlArray = [NSMutableArray array];
- for (NSInteger i = 0; i < controllerArray.count; i++) {
- CustomNavViewController *navCtrl = [self createNavControllerWithClassName:controllerArray[i] title:titleArray[i] imageName:imageArray[i] selectedImageName:selectedImgArray[i]];
- [ctrlArray addObject:navCtrl];
- }
- self.viewControllers = [NSArray arrayWithArray:ctrlArray];
- if (@available(iOS 13.0, *)) {
- [[UITabBar appearance] setUnselectedItemTintColor:HexRGB(0x999999)];
- [[UITabBar appearance] setTintColor:THEMECOLOR];
- } else {
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x999999),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:THEMECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
- }
- if (@available(iOS 15.0, *)) {
- // 适配Xcode 13 iOS 15tabbar透明的问题
- UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
- [appearance configureWithOpaqueBackground];
- appearance.backgroundColor = UIColor.whiteColor;
- appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
- [UITabBar appearance].scrollEdgeAppearance = appearance;
- [UITabBar appearance].standardAppearance = appearance;
- }
- [UITabBar appearance].backgroundColor = UIColor.whiteColor;
- self.tabBar.translucent = NO;
- }
- - (CustomNavViewController *)createNavControllerWithClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
- KSBaseViewController *controller = [[NSClassFromString(className) alloc] init];
- controller.title = title;
- controller.tabBarItem.title = title;
- controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x999999),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
- [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:THEMECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
- CustomNavViewController *navCtrl= [[CustomNavViewController alloc] initWithRootViewController:controller];
- return navCtrl;
- }
- #pragma mark ----- UITabBarControllerDelegate
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
- BOOL isDoubleClick = [self checkIsDoubleClick:viewController];
- if ([viewController isEqual:[tabBarController.viewControllers objectAtIndex:2]] && isDoubleClick) {
- [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESH object:nil];
- }
-
- [self setAnaimationWithTabBarController:tabBarController selectViewController:viewController];
- }
- - (void)setAnaimationWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
- //1.
- NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
- __block NSMutableArray <UIView *>*tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];
- //2.
- for (UIView *tempView in tabBarController.tabBar.subviews) {
- if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
- for (UIImageView *tempImageView in tempView.subviews) {
- if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
- [tabBarSwappableImageViews addObject:tempImageView];
- }
- }
- }
- }
- //3.
- __block UIView *currentTabBarSwappableImageView = tabBarSwappableImageViews[index];
- [AnimationHelper lottieAnimation:currentTabBarSwappableImageView index:index];
- }
- - (BOOL)checkIsDoubleClick:(UIViewController *)viewController {
- static UIViewController *lastViewController = nil;
- static NSTimeInterval lastClickTime = 0;
- if (lastViewController != viewController) {
-
- lastViewController = viewController;
- lastClickTime = [NSDate timeIntervalSinceReferenceDate];
- return NO;
- }
- NSTimeInterval clickTime = [NSDate timeIntervalSinceReferenceDate];
- if (clickTime - lastClickTime > 0.5) {
-
- lastClickTime = clickTime;
- return NO;
- }
- lastViewController = nil;
- lastClickTime = 0;
- return YES;
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- /*
- #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.
- }
- */
- @end
|