123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //
- // TSTabBarController.swift
- // TSLiveWallpaper
- //
- // Created by 100Years on 2024/12/20.
- //
- import UIKit
- class TSTabBarController: UITabBarController {
- private var viewControllerArray: [String] = []
- private var titleArray: [String] = []
- private var selectedImageArray: [String] = []
- private var unselectedImageArray: [String] = []
- private var markWidth = k_ScreenWidth / 2
- private lazy var markView: UIView = {
- let bgView = UIView()
- let colorView = UIView()
- colorView.backgroundColor = .themeColor
- bgView.addSubview(colorView)
- colorView.snp.makeConstraints { make in
- make.center.equalToSuperview()
- make.width.equalTo(15)
- make.height.equalTo(2)
- }
- return bgView
- }()
- override func viewDidLoad() {
- super.viewDidLoad()
- delegate = self
- setUpData()
- createUI()
- monitorEvent()
- }
- let dotSize:CGFloat = 6.0
- lazy var appUpdateDotView: UIView = {
- let bgView = UIView(frame: CGRect(x: 0, y: 0, width: dotSize, height: dotSize))
- let view = UIView()
- let backgroundColor = "#FF2B2B".uiColor
- view.backgroundColor = backgroundColor
- view.cornerRadius = dotSize/2.0
- view.addShadow(shadowColor: backgroundColor.cgColor, shadowOffset: CGSizeMake(0, 0), shadowRadius: 4, shadowOpacity: 0.4)
- bgView.addSubview(view)
- return view
- }()
-
- @objc private func setUpData() {
- viewControllerArray = [
- "TSAIListVC",
- "TSAIListHistoryVC",
- "TSMineVC"]
-
- selectedImageArray = [
- "tabbar_select_ailist",
- "tabbar_select_history",
- "tabbar_select_setting",
- ]
- unselectedImageArray = [
- "tabbar_unSelect_allist",
- "tabbar_unSelect_history",
- "tabbar_unSelect_setting",
- ]
- var tabArray: [UINavigationController] = []
- for i in 0 ..< viewControllerArray.count {
- if let rootViewController = viewControllerArray[i].toInstance(of: UIViewController.self) {
- let nav = TSBaseNavigationC(rootViewController: rootViewController)
- nav.view.backgroundColor = UIColor.black
- nav.tabBarItem = tabBarItem(
- title: "titleArray[i].localized",
- image: UIImage(named: unselectedImageArray[i]),
- selectedImage: UIImage(named: selectedImageArray[i]),
- tag: i
- )
- tabArray.append(nav)
- }
- }
- viewControllers = tabArray
- markWidth = k_ScreenWidth / CGFloat(tabArray.count)
- }
- private func createUI() {
- tabBar.barStyle = .black
- tabBar.isTranslucent = true
- if #available(iOS 13.0, *) {
- let appearance = UITabBarAppearance()
- appearance.backgroundEffect = UIBlurEffect(style: .dark) // 使用高斯模糊
- tabBar.standardAppearance = appearance
- if #available(iOS 15.0, *) {
- tabBar.scrollEdgeAppearance = appearance // 针对滚动边缘的外观
- }
- }
- tabBar.addSubview(markView)
- markView.snp.makeConstraints { make in
- make.bottom.equalTo(-k_Height_safeAreaInsetsBottom())
- make.leading.equalTo(0)
- make.width.equalTo(markWidth)
- make.height.equalTo(2)
- }
- }
-
- func monitorEvent(){
- refreshView()
- NotificationCenter.default.addObserver(self, selector: #selector(refreshView), name: .kAppUpdateNotification, object: nil)
- }
- let updateAlertVC = TSAppUpdateAlertVC()
- @objc func refreshView() {
- if TSAppUpdateManager.isNeedUpdate,TSAppUpdateManager.isDisplayedUpdateAlert == false{
- kMainAfter(1.0) {
- kPresentModalVC(target: self, modelVC: self.updateAlertVC,transitionStyle: .crossDissolve){
- TSAppUpdateManager.isDisplayedUpdateAlert = true
- }
- }
- }
- // showRedDotOnLastItem(show: TSAppUpdateManager.tabbarDotShow == 1)
- }
- deinit {
- debugPrint("TSTabBarController deinit")
- NotificationCenter.default.removeObserver(self)
- }
- }
- extension TSTabBarController {
- private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
- let tabBarItem = UITabBarItem()
- tabBarItem.image = image?.withRenderingMode(.alwaysOriginal).mirrored
- tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal).mirrored
- tabBarItem.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0) // 向下移动图标
- return tabBarItem
- }
- }
- extension TSTabBarController: UITabBarControllerDelegate {
- func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
- updateMarkViewFrame()
-
- if let viewControllers = viewControllers {
- if selectedIndex == viewControllers.count-1 {
- TSAppUpdateManager.tabbarDotShow = 2
- showRedDotOnLastItem(show: false)
- }
- }
- }
- }
- extension TSTabBarController {
- func showRedDotOnLastItem(show: Bool) {
- guard let items = tabBar.items, !items.isEmpty else { return }
- if show {
- addRedDotToLastTab()
- } else {
- appUpdateDotView.isHidden = true
- }
- }
-
- func addRedDotToLastTab() {
- guard let items = tabBar.items,
- !items.isEmpty else { return }
-
- if appUpdateDotView.superview != nil {
- appUpdateDotView.isHidden = false
- return
- }
-
- let lastItemIndex = items.count - 1
- let itemWidth = tabBar.frame.width / CGFloat(items.count)
- let xPosition = itemWidth * CGFloat(lastItemIndex) + itemWidth/2 + (dotSize * 2) + 2
- let yPosition: CGFloat = 10
- tabBar.addSubview(appUpdateDotView)
- appUpdateDotView.snp.makeConstraints { make in
- make.width.height.equalTo(dotSize)
- make.leading.equalTo(xPosition)
- make.top.equalTo(yPosition)
- }
- }
-
- func changeSelectedIndex(index:Int){
- self.selectedIndex = index
- updateMarkViewFrame()
- }
-
- func updateMarkViewFrame() {
- markView.snp.updateConstraints { make in
- make.leading.equalTo(CGFloat(selectedIndex) * markWidth)
- }
- }
-
- }
- extension TSTabBarController {
- /// 暂时没用,后续迭代
- func scheduleDailyNotification() {
- // 创建通知内容
- let content = UNMutableNotificationContent()
- content.title = "🎧 ≡ ◁◁ ❚❚ ▷▷ ↻"
- content.body = "♬ ♬ ♩ ♡ ♪ ♪ ♫ ♭ ♫ ♡"
- content.sound = UNNotificationSound.default
- // 创建通知触发条件
- var dateComponents = DateComponents()
- dateComponents.hour = 21 // 24小时制,20点即晚上8点
- let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
- // 创建通知请求
- let request = UNNotificationRequest(identifier: "DailyNotification", content: content, trigger: trigger)
- // 将通知添加到通知中心
- UNUserNotificationCenter.current().add(request) { error in
- if let error = error {
- print("添加通知失败: \(error.localizedDescription)")
- }
- }
- }
- }
|