TSTabBarController.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // TSTabBarController.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. import UIKit
  8. class TSTabBarController: UITabBarController {
  9. private var viewControllerArray: [String] = []
  10. private var titleArray: [String] = []
  11. private var selectedImageArray: [String] = []
  12. private var unselectedImageArray: [String] = []
  13. private var markWidth = k_ScreenWidth / 2
  14. private lazy var markView: UIView = {
  15. let bgView = UIView()
  16. let colorView = UIView()
  17. colorView.backgroundColor = .themeColor
  18. bgView.addSubview(colorView)
  19. colorView.snp.makeConstraints { make in
  20. make.center.equalToSuperview()
  21. make.width.equalTo(15)
  22. make.height.equalTo(2)
  23. }
  24. return bgView
  25. }()
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. delegate = self
  29. setUpData()
  30. createUI()
  31. monitorEvent()
  32. }
  33. let dotSize:CGFloat = 6.0
  34. lazy var appUpdateDotView: UIView = {
  35. let bgView = UIView(frame: CGRect(x: 0, y: 0, width: dotSize, height: dotSize))
  36. let view = UIView()
  37. let backgroundColor = "#FF2B2B".uiColor
  38. view.backgroundColor = backgroundColor
  39. view.cornerRadius = dotSize/2.0
  40. view.addShadow(shadowColor: backgroundColor.cgColor, shadowOffset: CGSizeMake(0, 0), shadowRadius: 4, shadowOpacity: 0.4)
  41. bgView.addSubview(view)
  42. return view
  43. }()
  44. @objc private func setUpData() {
  45. viewControllerArray = [
  46. "TSAIListVC",
  47. "TSAIListHistoryVC",
  48. "TSMineVC"]
  49. selectedImageArray = [
  50. "tabbar_select_ailist",
  51. "tabbar_select_history",
  52. "tabbar_select_setting",
  53. ]
  54. unselectedImageArray = [
  55. "tabbar_unSelect_allist",
  56. "tabbar_unSelect_history",
  57. "tabbar_unSelect_setting",
  58. ]
  59. var tabArray: [UINavigationController] = []
  60. for i in 0 ..< viewControllerArray.count {
  61. if let rootViewController = viewControllerArray[i].toInstance(of: UIViewController.self) {
  62. let nav = TSBaseNavigationC(rootViewController: rootViewController)
  63. nav.view.backgroundColor = UIColor.black
  64. nav.tabBarItem = tabBarItem(
  65. title: "titleArray[i].localized",
  66. image: UIImage(named: unselectedImageArray[i]),
  67. selectedImage: UIImage(named: selectedImageArray[i]),
  68. tag: i
  69. )
  70. tabArray.append(nav)
  71. }
  72. }
  73. viewControllers = tabArray
  74. markWidth = k_ScreenWidth / CGFloat(tabArray.count)
  75. }
  76. private func createUI() {
  77. tabBar.barStyle = .black
  78. tabBar.isTranslucent = true
  79. if #available(iOS 13.0, *) {
  80. let appearance = UITabBarAppearance()
  81. appearance.backgroundEffect = UIBlurEffect(style: .dark) // 使用高斯模糊
  82. tabBar.standardAppearance = appearance
  83. if #available(iOS 15.0, *) {
  84. tabBar.scrollEdgeAppearance = appearance // 针对滚动边缘的外观
  85. }
  86. }
  87. tabBar.addSubview(markView)
  88. markView.snp.makeConstraints { make in
  89. make.bottom.equalTo(-k_Height_safeAreaInsetsBottom())
  90. make.leading.equalTo(0)
  91. make.width.equalTo(markWidth)
  92. make.height.equalTo(2)
  93. }
  94. }
  95. func monitorEvent(){
  96. refreshView()
  97. NotificationCenter.default.addObserver(self, selector: #selector(refreshView), name: .kAppUpdateNotification, object: nil)
  98. }
  99. let updateAlertVC = TSAppUpdateAlertVC()
  100. @objc func refreshView() {
  101. if TSAppUpdateManager.isNeedUpdate,TSAppUpdateManager.isDisplayedUpdateAlert == false{
  102. kMainAfter(1.0) {
  103. kPresentModalVC(target: self, modelVC: self.updateAlertVC,transitionStyle: .crossDissolve){
  104. TSAppUpdateManager.isDisplayedUpdateAlert = true
  105. }
  106. }
  107. }
  108. // showRedDotOnLastItem(show: TSAppUpdateManager.tabbarDotShow == 1)
  109. }
  110. deinit {
  111. debugPrint("TSTabBarController deinit")
  112. NotificationCenter.default.removeObserver(self)
  113. }
  114. }
  115. extension TSTabBarController {
  116. private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
  117. let tabBarItem = UITabBarItem()
  118. tabBarItem.image = image?.withRenderingMode(.alwaysOriginal).mirrored
  119. tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal).mirrored
  120. tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0) // 向下移动图标
  121. return tabBarItem
  122. }
  123. }
  124. extension TSTabBarController: UITabBarControllerDelegate {
  125. func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
  126. updateMarkViewFrame()
  127. if let viewControllers = viewControllers {
  128. if selectedIndex == viewControllers.count-1 {
  129. TSAppUpdateManager.tabbarDotShow = 2
  130. showRedDotOnLastItem(show: false)
  131. }
  132. }
  133. }
  134. }
  135. extension TSTabBarController {
  136. func showRedDotOnLastItem(show: Bool) {
  137. guard let items = tabBar.items, !items.isEmpty else { return }
  138. if show {
  139. addRedDotToLastTab()
  140. } else {
  141. appUpdateDotView.isHidden = true
  142. }
  143. }
  144. func addRedDotToLastTab() {
  145. guard let items = tabBar.items,
  146. !items.isEmpty else { return }
  147. if appUpdateDotView.superview != nil {
  148. appUpdateDotView.isHidden = false
  149. return
  150. }
  151. let lastItemIndex = items.count - 1
  152. let itemWidth = tabBar.frame.width / CGFloat(items.count)
  153. let xPosition = itemWidth * CGFloat(lastItemIndex) + itemWidth/2 + (dotSize * 2) + 2
  154. let yPosition: CGFloat = 10
  155. tabBar.addSubview(appUpdateDotView)
  156. appUpdateDotView.snp.makeConstraints { make in
  157. make.width.height.equalTo(dotSize)
  158. make.leading.equalTo(xPosition)
  159. make.top.equalTo(yPosition)
  160. }
  161. }
  162. func changeSelectedIndex(index:Int){
  163. self.selectedIndex = index
  164. updateMarkViewFrame()
  165. }
  166. func updateMarkViewFrame() {
  167. markView.snp.updateConstraints { make in
  168. make.leading.equalTo(CGFloat(selectedIndex) * markWidth)
  169. }
  170. }
  171. }
  172. extension TSTabBarController {
  173. /// 暂时没用,后续迭代
  174. func scheduleDailyNotification() {
  175. // 创建通知内容
  176. let content = UNMutableNotificationContent()
  177. content.title = "🎧 ≡ ◁◁ ❚❚ ▷▷ ↻"
  178. content.body = "♬ ♬ ♩ ♡ ♪ ♪ ♫ ♭ ♫ ♡"
  179. content.sound = UNNotificationSound.default
  180. // 创建通知触发条件
  181. var dateComponents = DateComponents()
  182. dateComponents.hour = 21 // 24小时制,20点即晚上8点
  183. let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
  184. // 创建通知请求
  185. let request = UNNotificationRequest(identifier: "DailyNotification", content: content, trigger: trigger)
  186. // 将通知添加到通知中心
  187. UNUserNotificationCenter.current().add(request) { error in
  188. if let error = error {
  189. print("添加通知失败: \(error.localizedDescription)")
  190. }
  191. }
  192. }
  193. }