KSTabBarViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // KSTabBarViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/17.
  6. //
  7. #import "KSTabBarViewController.h"
  8. #import "CustomNavViewController.h"
  9. #import "KSBaseViewController.h"
  10. #import <UIImage+Color.h>
  11. #import "AnimationHelper.h"
  12. #import "MineWorksViewController.h"
  13. #import "KSBaseWKWebViewController.h"
  14. @interface KSTabBarViewController ()<UITabBarControllerDelegate>
  15. @end
  16. @implementation KSTabBarViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20. self.delegate = self;
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDraftPage) name:@"DisplayDraft" object:nil];
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showWorksPage:) name:@"displayWorks" object:nil];
  23. [self configItems];
  24. }
  25. - (void)showWorksPage:(NSNotification *)notification {
  26. [self showWorksDraft:NO info:notification.object];
  27. }
  28. - (void)showWorksDraft:(BOOL)isDraft info:(NSDictionary *)info {
  29. [self tabBarSelectedWithIndex:4];
  30. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  31. CustomNavViewController *navCtrl = self.selectedViewController;
  32. MineWorksViewController *ctrl = [[MineWorksViewController alloc] init];
  33. [ctrl displayDraft:isDraft];
  34. [navCtrl pushViewController:ctrl animated:YES];
  35. if (isDraft == NO) {
  36. // 显示作品详情
  37. NSString *url = [info ks_stringValueForKey:@"url"];
  38. if (![NSString isEmptyString:url]) {
  39. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  40. ctrl.url = url;
  41. [navCtrl pushViewController:ctrl animated:YES];
  42. }
  43. }
  44. });
  45. }
  46. - (void)showDraftPage {
  47. [self showWorksDraft:YES info:nil];
  48. }
  49. - (void)configItems {
  50. // 处理 pad上 tabbar样式
  51. if (@available(iOS 18.0, *)) {
  52. if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
  53. {
  54. self.traitOverrides.horizontalSizeClass = UIUserInterfaceSizeClassCompact; //TAB BAR ON BOTTOM
  55. }
  56. }
  57. NSArray *controllerArray = @[@"HomeViewController",@"CourseViewController",@"ChatViewController",@"ShopMallViewController",@"MineViewController"];
  58. NSArray *titleArray = @[@"首页",@"课表",@"聊天",@"商城",@"我的"];
  59. NSArray *imageArray = @[@"tab_home_unselected",@"tab_course_unselected",@"tab_chat_unselected",@"tab_mall_unselected",@"tab_mine_unselected"];
  60. NSArray *selectedImgArray = @[@"tab_home_selected",@"tab_course_selected",@"tab_chat_selected",@"tab_mall_selected",@"tab_mine_selected"];
  61. NSMutableArray *ctrlArray = [NSMutableArray array];
  62. for (NSInteger i = 0; i < controllerArray.count; i++) {
  63. CustomNavViewController *navCtrl = [self createNavControllerWithClassName:controllerArray[i] title:titleArray[i] imageName:imageArray[i] selectedImageName:selectedImgArray[i]];
  64. [ctrlArray addObject:navCtrl];
  65. }
  66. self.viewControllers = [NSArray arrayWithArray:ctrlArray];
  67. if (@available(iOS 13.0, *)) {
  68. [[UITabBar appearance] setUnselectedItemTintColor:HexRGB(0x999999)];
  69. [[UITabBar appearance] setTintColor:THEMECOLOR];
  70. } else {
  71. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x999999),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
  72. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:THEMECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
  73. }
  74. if (@available(iOS 15.0, *)) {
  75. // 适配Xcode 13 iOS 15tabbar透明的问题
  76. UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
  77. [appearance configureWithOpaqueBackground];
  78. appearance.backgroundColor = UIColor.whiteColor;
  79. appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
  80. [UITabBar appearance].scrollEdgeAppearance = appearance;
  81. [UITabBar appearance].standardAppearance = appearance;
  82. }
  83. [UITabBar appearance].backgroundColor = UIColor.whiteColor;
  84. self.tabBar.translucent = NO;
  85. }
  86. - (CustomNavViewController *)createNavControllerWithClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
  87. KSBaseViewController *controller = [[NSClassFromString(className) alloc] init];
  88. controller.title = title;
  89. controller.tabBarItem.title = title;
  90. controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  91. controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  92. [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x999999),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
  93. [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:THEMECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
  94. CustomNavViewController *navCtrl= [[CustomNavViewController alloc] initWithRootViewController:controller];
  95. return navCtrl;
  96. }
  97. #pragma mark ----- UITabBarControllerDelegate
  98. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  99. BOOL isDoubleClick = [self checkIsDoubleClick:viewController];
  100. if ([viewController isEqual:[tabBarController.viewControllers objectAtIndex:2]] && isDoubleClick) {
  101. [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESH object:nil];
  102. }
  103. [self setAnaimationWithTabBarController:tabBarController selectViewController:viewController];
  104. }
  105. - (void)setAnaimationWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
  106. //1.
  107. NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
  108. __block NSMutableArray <UIView *>*tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];
  109. //2.
  110. for (UIView *tempView in tabBarController.tabBar.subviews) {
  111. if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  112. for (UIImageView *tempImageView in tempView.subviews) {
  113. if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
  114. [tabBarSwappableImageViews addObject:tempImageView];
  115. }
  116. }
  117. }
  118. }
  119. //3.
  120. __block UIView *currentTabBarSwappableImageView = tabBarSwappableImageViews[index];
  121. [AnimationHelper lottieAnimation:currentTabBarSwappableImageView index:index];
  122. }
  123. - (BOOL)checkIsDoubleClick:(UIViewController *)viewController {
  124. static UIViewController *lastViewController = nil;
  125. static NSTimeInterval lastClickTime = 0;
  126. if (lastViewController != viewController) {
  127. lastViewController = viewController;
  128. lastClickTime = [NSDate timeIntervalSinceReferenceDate];
  129. return NO;
  130. }
  131. NSTimeInterval clickTime = [NSDate timeIntervalSinceReferenceDate];
  132. if (clickTime - lastClickTime > 0.5) {
  133. lastClickTime = clickTime;
  134. return NO;
  135. }
  136. lastViewController = nil;
  137. lastClickTime = 0;
  138. return YES;
  139. }
  140. - (void)dealloc {
  141. [[NSNotificationCenter defaultCenter] removeObserver:self];
  142. }
  143. /*
  144. #pragma mark - Navigation
  145. // In a storyboard-based application, you will often want to do a little preparation before navigation
  146. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  147. // Get the new view controller using [segue destinationViewController].
  148. // Pass the selected object to the new view controller.
  149. }
  150. */
  151. @end