TSTabBarController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // TSTabBarController.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. import Kingfisher
  8. import UIKit
  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])
  15. // }
  16. // return animatedImageView
  17. // }()
  18. private var viewControllerArray: [String] = []
  19. private var titleArray: [String] = []
  20. private var selectedImageArray: [String] = []
  21. private var unselectedImageArray: [String] = []
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. createUI()
  25. setUpData()
  26. monitorEvent()
  27. // addAnimatedImageView()
  28. }
  29. @objc private func setUpData() {
  30. viewControllerArray = ["TSDiscoverVC", "TSGenerateHistoryVC", "TSSetingVC"]
  31. titleArray = ["Discover", "Creations", "Setting"]
  32. selectedImageArray = [
  33. "tabbar_select_pic",
  34. "tabbar_select_history",
  35. "tabbar_select_setting",
  36. ]
  37. unselectedImageArray = [
  38. "tabbar_unSelect_pic",
  39. "tabbar_unSelect_history",
  40. "tabbar_unSelect_setting",
  41. ]
  42. var tabArray: [UINavigationController] = []
  43. for i in 0 ..< viewControllerArray.count {
  44. if let rootViewController = viewControllerArray[i].toInstance(of: UIViewController.self) {
  45. let title = titleArray[i]
  46. let nav = TSBaseNavigationC(rootViewController: rootViewController)
  47. nav.view.backgroundColor = UIColor.black
  48. nav.tabBarItem = tabBarItem(
  49. title: title.localized,
  50. image: UIImage(named: unselectedImageArray[i]),
  51. selectedImage: UIImage(named: selectedImageArray[i]),
  52. tag: i
  53. )
  54. tabArray.append(nav)
  55. }
  56. }
  57. viewControllers = tabArray
  58. }
  59. private func createUI() {
  60. tabBar.barStyle = .black
  61. tabBar.isTranslucent = false
  62. tabBar.backgroundColor = "#262626".uiColor
  63. tabBar.backgroundImage = UIImage.colorFrom(color: tabBar.backgroundColor!, size: CGSize(width: k_ScreenWidth, height: k_TabBar_Height))
  64. // 自定义 TabBarItem 字体颜色和选中颜色
  65. UITabBar.appearance().unselectedItemTintColor = .white.withAlphaComponent(0.4) // 未选中颜色
  66. UITabBar.appearance().tintColor = UIColor.themeColor // 选中颜色
  67. delegate = self
  68. // Workaround to fix the tab bar position in iPad
  69. if #available(iOS 18.0, *), UIDevice.current.userInterfaceIdiom == .pad {
  70. tabBar.removeFromSuperview()
  71. view.addSubview(tabBar)
  72. traitOverrides.horizontalSizeClass = .compact
  73. }
  74. setupDoubleTapToScrollTop()
  75. }
  76. private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
  77. let tabBarItem = UITabBarItem()
  78. tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
  79. tabBarItem.title = title
  80. tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
  81. tabBarItem.setTitleTextAttributes([
  82. .font: UIFont.font(size: 12),
  83. ], for: .normal)
  84. tabBarItem.setTitleTextAttributes([
  85. .font: UIFont.font(size: 12),
  86. ], for: .selected)
  87. return tabBarItem
  88. }
  89. func monitorEvent() {
  90. // 后台生成后,跳转到历史页面
  91. NotificationCenter.default.addObserver(forName: .kAIComeInBackstage, object: nil, queue: .main) { _ in
  92. if let nav = self.viewControllers?.safeObj(At: self.selectedIndex) as? UINavigationController {
  93. nav.popToRootViewController(animated: false)
  94. }
  95. TSGenerateHistoryVC.showPosition()
  96. }
  97. // 升级弹窗
  98. refreshUpdateView()
  99. NotificationCenter.default.addObserver(self, selector: #selector(refreshUpdateView), name: .kAppUpdateNotification, object: nil)
  100. // 关闭订阅页面,弹出优惠订阅
  101. refreshPurchasePromotional()
  102. NotificationCenter.default.addObserver(self, selector: #selector(refreshPurchasePromotional), name: .kCloseTSPurchaseVC, object: nil)
  103. debugPrint("TSTabBarController self = \(self)")
  104. }
  105. deinit {
  106. debugPrint("TSTabBarController deinit")
  107. NotificationCenter.default.removeObserver(self)
  108. }
  109. }
  110. extension TSTabBarController {
  111. func changeSelectedIndex(index: Int) {
  112. selectedIndex = index
  113. }
  114. }
  115. // 升级弹窗
  116. extension TSTabBarController {
  117. @objc func refreshUpdateView() {
  118. if TSAppUpdateManager.isNeedUpdate, TSAppUpdateManager.isDisplayedUpdateAlert == false {
  119. kMainAfter(1.0) {
  120. let updateAlertVC = TSAppUpdateAlertVC()
  121. updateAlertVC.modalPresentationStyle = .overFullScreen
  122. updateAlertVC.modalTransitionStyle = .crossDissolve
  123. self.present(updateAlertVC, animated: true, completion: {
  124. TSAppUpdateManager.isDisplayedUpdateAlert = true
  125. })
  126. }
  127. }
  128. }
  129. }
  130. // 关闭订阅页面,弹出优惠订阅
  131. extension TSTabBarController {
  132. @objc func refreshPurchasePromotional() {
  133. // if TSPurchasePromotionalVC.showGift, kPurchaseCountDownTime.isNeedCheck == true {
  134. // kMainAfter(0.3) {
  135. // let updateAlertVC = TSPurchasePromotionalVC(isAnimation: true)
  136. // updateAlertVC.modalPresentationStyle = .overFullScreen
  137. // updateAlertVC.modalTransitionStyle = .crossDissolve
  138. // self.present(updateAlertVC, animated: true, completion: {
  139. // kPurchaseCountDownTime.isNeedCheck = false
  140. // })
  141. // }
  142. // }
  143. }
  144. }
  145. // private let animatedIndex = 1
  146. // extension TSTabBarController {
  147. // func addAnimatedImageView(){
  148. //
  149. // let index = 1
  150. // if let tabBarItems = tabBar.items,tabBarItems.count > index {
  151. // let tabBarButtons = tabBar.subviews.filter({ $0.isKind(of: NSClassFromString("UITabBarButton")!) })
  152. // if tabBarButtons.count > index {
  153. // let thirdTabButton = tabBarButtons[index]
  154. // thirdTabButton.addSubview(animatedImageView)
  155. // animatedImageView.snp.makeConstraints { make in
  156. // make.width.equalTo(13.0)
  157. // make.height.equalTo(22.0)
  158. // make.centerX.equalToSuperview().offset(11)
  159. // make.top.equalTo(6)
  160. // }
  161. // kDelayMainShort {
  162. // self.animatedImageView.startAnimating()
  163. // }
  164. //
  165. // }
  166. // }
  167. // }
  168. // }
  169. extension TSTabBarController: UITabBarControllerDelegate {
  170. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  171. // if tabBarController.selectedIndex == animatedIndex {
  172. // self.animatedImageView.isHidden = true
  173. // }else{
  174. // self.animatedImageView.isHidden = false
  175. // }
  176. }
  177. }
  178. protocol TSTabBarControllerProtocol {
  179. func tabBarDoubleTap(_ tabbar: UITabBarController)
  180. }
  181. extension TSTabBarController {
  182. func setupDoubleTapToScrollTop() {
  183. let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
  184. doubleTap.numberOfTapsRequired = 2
  185. doubleTap.delaysTouchesBegan = false // 默认是true,会延迟单击响应
  186. doubleTap.delaysTouchesEnded = false
  187. tabBar.addGestureRecognizer(doubleTap)
  188. }
  189. @objc private func handleDoubleTap(_ gesture: UITapGestureRecognizer) {
  190. guard gesture.state == .ended else { return }
  191. guard let vc = selectedViewController else { return }
  192. if let vc = vc as? TSTabBarControllerProtocol {
  193. vc.tabBarDoubleTap(self)
  194. } else if let nav = vc as? UINavigationController {
  195. if let topVC = nav.topViewController {
  196. if let vc = topVC as? TSTabBarControllerProtocol {
  197. vc.tabBarDoubleTap(self)
  198. }
  199. }
  200. }
  201. }
  202. }