TSTabBarController.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // TSTabBarController.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. import UIKit
  8. import Kingfisher
  9. class TSTabBarController: UITabBarController {
  10. lazy var animatedImageView: AnimatedImageView = {
  11. let animatedImageView = AnimatedImageView()
  12. animatedImageView.autoPlayAnimatedImage = false
  13. if let gifURL = Bundle.main.url(forResource: "catPaw_left", withExtension: "gif") {
  14. animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]) { result in
  15. switch result {
  16. case .success(let value):
  17. print("GIF 加载成功: \(value.source.url?.absoluteString ?? "")")
  18. case .failure(let error):
  19. print("GIF 加载失败: \(error.localizedDescription)")
  20. }
  21. }
  22. }
  23. return animatedImageView
  24. }()
  25. private var viewControllerArray: [String] = []
  26. private var titleArray: [String] = []
  27. private var selectedImageArray: [String] = []
  28. private var unselectedImageArray: [String] = []
  29. private var interceptVC: UINavigationController = UINavigationController()
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. createUI()
  33. setUpData()
  34. addAnimatedImageView()
  35. }
  36. @objc private func setUpData() {
  37. viewControllerArray = ["TSPTPInputVC","TSAILIstVC","TSTTPInputVC","TSGenerateHistoryVC","TSSetingVC"]
  38. titleArray = ["Photo","AI Store","Text","Creations","Setting"]
  39. selectedImageArray = [
  40. "tabbar_select_pic",
  41. "tabbar_select_ailist",
  42. "tabbar_select_text",
  43. "tabbar_select_aichat",
  44. "tabbar_select_setting"
  45. ]
  46. unselectedImageArray = [
  47. "tabbar_unSelect_pic",
  48. "tabbar_unSelect_ailist",
  49. "tabbar_unSelect_text",
  50. "tabbar_unSelect_aichat",
  51. "tabbar_unSelect_setting"
  52. ]
  53. var tabArray: [UINavigationController] = []
  54. for i in 0..<viewControllerArray.count {
  55. if let rootViewController = viewControllerArray[i].toInstance(of: UIViewController.self) {
  56. let title = titleArray[i]
  57. let nav = TSBaseNavigationC(rootViewController: rootViewController)
  58. nav.view.backgroundColor = UIColor.black
  59. nav.tabBarItem = tabBarItem(
  60. title: title.localized,
  61. image: UIImage(named: unselectedImageArray[i]),
  62. selectedImage: UIImage(named: selectedImageArray[i]),
  63. tag: i
  64. )
  65. tabArray.append(nav)
  66. }
  67. }
  68. self.viewControllers = tabArray
  69. interceptVC = tabArray.last!
  70. }
  71. private func createUI() {
  72. tabBar.barStyle = .black
  73. tabBar.isTranslucent = false
  74. tabBar.backgroundColor = "#262626".uiColor
  75. tabBar.backgroundImage = UIImage.colorFrom(color: tabBar.backgroundColor!, size: CGSize(width: k_ScreenWidth, height: k_TabBar_Height))
  76. // 自定义 TabBarItem 字体颜色和选中颜色
  77. UITabBar.appearance().unselectedItemTintColor = .white.withAlphaComponent(0.4)// 未选中颜色
  78. UITabBar.appearance().tintColor = UIColor.themeColor // 选中颜色
  79. delegate = self
  80. }
  81. private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
  82. let tabBarItem = UITabBarItem()
  83. tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
  84. tabBarItem.title = title
  85. tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
  86. // tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0) // 向下移动图标
  87. return tabBarItem
  88. }
  89. deinit {
  90. debugPrint("TSTabBarController deinit")
  91. NotificationCenter.default.removeObserver(self)
  92. }
  93. }
  94. private let animatedIndex = 1
  95. extension TSTabBarController {
  96. func addAnimatedImageView(){
  97. let index = 1
  98. if let tabBarItems = tabBar.items,tabBarItems.count > index {
  99. let tabBarButtons = tabBar.subviews.filter({ $0.isKind(of: NSClassFromString("UITabBarButton")!) })
  100. if tabBarButtons.count > index {
  101. let thirdTabButton = tabBarButtons[index]
  102. thirdTabButton.addSubview(animatedImageView)
  103. animatedImageView.snp.makeConstraints { make in
  104. make.width.equalTo(13.0)
  105. make.height.equalTo(22.0)
  106. make.centerX.equalToSuperview().offset(11)
  107. make.top.equalTo(6)
  108. }
  109. kDelayMainShort {
  110. self.animatedImageView.startAnimating()
  111. }
  112. }
  113. }
  114. }
  115. func changeSelectedIndex(index:Int){
  116. self.selectedIndex = index
  117. }
  118. }
  119. extension TSTabBarController:UITabBarControllerDelegate {
  120. // func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool{
  121. // if interceptVC == viewController{
  122. // kPresentModalVC(target: self, modelVC: TSWallpaperVC())
  123. // return false
  124. // }
  125. // return true
  126. // }
  127. // UITabBarControllerDelegate 方法
  128. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  129. if tabBarController.selectedIndex == animatedIndex {
  130. self.animatedImageView.isHidden = true
  131. }else{
  132. self.animatedImageView.isHidden = false
  133. }
  134. }
  135. }