TSTabBarController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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])
  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. self.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. }
  104. deinit {
  105. debugPrint("TSTabBarController deinit")
  106. NotificationCenter.default.removeObserver(self)
  107. }
  108. }
  109. extension TSTabBarController {
  110. func changeSelectedIndex(index:Int){
  111. self.selectedIndex = index
  112. }
  113. }
  114. //升级弹窗
  115. extension TSTabBarController {
  116. @objc func refreshUpdateView() {
  117. if TSAppUpdateManager.isNeedUpdate,TSAppUpdateManager.isDisplayedUpdateAlert == false{
  118. kMainAfter(1.0) {
  119. let updateAlertVC = TSAppUpdateAlertVC()
  120. updateAlertVC.modalPresentationStyle = .overFullScreen
  121. updateAlertVC.modalTransitionStyle = .crossDissolve
  122. self.present(updateAlertVC, animated: true,completion: {
  123. TSAppUpdateManager.isDisplayedUpdateAlert = true
  124. })
  125. }
  126. }
  127. }
  128. }
  129. //关闭订阅页面,弹出优惠订阅
  130. extension TSTabBarController {
  131. @objc func refreshPurchasePromotional() {
  132. if TSPurchasePromotionalVC.showGift,kPurchaseCountDownTime.isNeedCheck == true{
  133. kMainAfter(0.3) {
  134. let updateAlertVC = TSPurchasePromotionalVC(isAnimation: true)
  135. updateAlertVC.modalPresentationStyle = .overFullScreen
  136. updateAlertVC.modalTransitionStyle = .crossDissolve
  137. self.present(updateAlertVC, animated: true,completion: {
  138. kPurchaseCountDownTime.isNeedCheck = false
  139. })
  140. }
  141. }
  142. }
  143. }
  144. //private let animatedIndex = 1
  145. //extension TSTabBarController {
  146. // func addAnimatedImageView(){
  147. //
  148. // let index = 1
  149. // if let tabBarItems = tabBar.items,tabBarItems.count > index {
  150. // let tabBarButtons = tabBar.subviews.filter({ $0.isKind(of: NSClassFromString("UITabBarButton")!) })
  151. // if tabBarButtons.count > index {
  152. // let thirdTabButton = tabBarButtons[index]
  153. // thirdTabButton.addSubview(animatedImageView)
  154. // animatedImageView.snp.makeConstraints { make in
  155. // make.width.equalTo(13.0)
  156. // make.height.equalTo(22.0)
  157. // make.centerX.equalToSuperview().offset(11)
  158. // make.top.equalTo(6)
  159. // }
  160. // kDelayMainShort {
  161. // self.animatedImageView.startAnimating()
  162. // }
  163. //
  164. // }
  165. // }
  166. // }
  167. //}
  168. extension TSTabBarController:UITabBarControllerDelegate {
  169. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  170. // if tabBarController.selectedIndex == animatedIndex {
  171. // self.animatedImageView.isHidden = true
  172. // }else{
  173. // self.animatedImageView.isHidden = false
  174. // }
  175. }
  176. }
  177. protocol TSTabBarControllerProtocol {
  178. func tabBarDoubleTap(_ tabbar: UITabBarController)
  179. }
  180. extension TSTabBarController {
  181. func setupDoubleTapToScrollTop() {
  182. let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
  183. doubleTap.numberOfTapsRequired = 2
  184. doubleTap.delaysTouchesBegan = false // 默认是true,会延迟单击响应
  185. doubleTap.delaysTouchesEnded = false
  186. tabBar.addGestureRecognizer(doubleTap)
  187. }
  188. @objc private func handleDoubleTap(_ gesture: UITapGestureRecognizer) {
  189. guard gesture.state == .ended else { return }
  190. guard let vc = selectedViewController else { return }
  191. if let vc = vc as? TSTabBarControllerProtocol{
  192. vc.tabBarDoubleTap(self)
  193. }else if let nav = vc as? UINavigationController {
  194. if let topVC = nav.topViewController {
  195. if let vc = topVC as? TSTabBarControllerProtocol{
  196. vc.tabBarDoubleTap(self)
  197. }
  198. }
  199. }
  200. }
  201. }