123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- //
- // TSTabBarController.swift
- // TSLiveWallpaper
- //
- // Created by 100Years on 2024/12/20.
- //
- import UIKit
- import Kingfisher
- class TSTabBarController: UITabBarController {
-
- // lazy var animatedImageView: AnimatedImageView = {
- // let animatedImageView = AnimatedImageView()
- // animatedImageView.autoPlayAnimatedImage = false
- // if let gifURL = Bundle.main.url(forResource: "catPaw_left", withExtension: "gif") {
- // animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage])
- // }
- // return animatedImageView
- // }()
-
- private var viewControllerArray: [String] = []
- private var titleArray: [String] = []
- private var selectedImageArray: [String] = []
- private var unselectedImageArray: [String] = []
-
- override func viewDidLoad() {
- super.viewDidLoad()
- createUI()
- setUpData()
- monitorEvent()
- // addAnimatedImageView()
- }
- @objc private func setUpData() {
- viewControllerArray = ["TSDiscoverVC","TSGenerateHistoryVC","TSSetingVC"]
- titleArray = ["Discover","Creations","Setting"]
- selectedImageArray = [
- "tabbar_select_pic",
- "tabbar_select_history",
- "tabbar_select_setting"
- ]
- unselectedImageArray = [
- "tabbar_unSelect_pic",
- "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 title = titleArray[i]
- let nav = TSBaseNavigationC(rootViewController: rootViewController)
- nav.view.backgroundColor = UIColor.black
- nav.tabBarItem = tabBarItem(
- title: title.localized,
- image: UIImage(named: unselectedImageArray[i]),
- selectedImage: UIImage(named: selectedImageArray[i]),
- tag: i
- )
- tabArray.append(nav)
- }
- }
- self.viewControllers = tabArray
- }
- private func createUI() {
- tabBar.barStyle = .black
- tabBar.isTranslucent = false
- tabBar.backgroundColor = "#262626".uiColor
- tabBar.backgroundImage = UIImage.colorFrom(color: tabBar.backgroundColor!, size: CGSize(width: k_ScreenWidth, height: k_TabBar_Height))
-
- // 自定义 TabBarItem 字体颜色和选中颜色
- UITabBar.appearance().unselectedItemTintColor = .white.withAlphaComponent(0.4)// 未选中颜色
- UITabBar.appearance().tintColor = UIColor.themeColor // 选中颜色
-
- delegate = self
-
- // Workaround to fix the tab bar position in iPad
- if #available(iOS 18.0, *), UIDevice.current.userInterfaceIdiom == .pad {
- tabBar.removeFromSuperview()
- view.addSubview(tabBar)
- traitOverrides.horizontalSizeClass = .compact
- }
-
- setupDoubleTapToScrollTop()
- }
- private func tabBarItem(title: String, image: UIImage?, selectedImage: UIImage?, tag: Int) -> UITabBarItem {
- let tabBarItem = UITabBarItem()
- tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
- tabBarItem.title = title
- tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
-
- tabBarItem.setTitleTextAttributes([
- .font: UIFont.font(size: 12),
- ],for: .normal)
-
- tabBarItem.setTitleTextAttributes([
- .font: UIFont.font(size: 12),
- ], for: .selected)
-
- return tabBarItem
- }
- func monitorEvent(){
-
- //后台生成后,跳转到历史页面
- NotificationCenter.default.addObserver(forName: .kAIComeInBackstage, object: nil, queue: .main) { _ in
- if let nav = self.viewControllers?.safeObj(At: self.selectedIndex) as? UINavigationController {
- nav.popToRootViewController(animated: false)
- }
- TSGenerateHistoryVC.showPosition()
- }
-
- //升级弹窗
- refreshUpdateView()
- NotificationCenter.default.addObserver(self, selector: #selector(refreshUpdateView), name: .kAppUpdateNotification, object: nil)
-
- //关闭订阅页面,弹出优惠订阅
- refreshPurchasePromotional()
- NotificationCenter.default.addObserver(self, selector: #selector(refreshPurchasePromotional), name: .kCloseTSPurchaseVC, object: nil)
- }
-
- deinit {
- debugPrint("TSTabBarController deinit")
- NotificationCenter.default.removeObserver(self)
- }
- }
- extension TSTabBarController {
- func changeSelectedIndex(index:Int){
- self.selectedIndex = index
- }
- }
- //升级弹窗
- extension TSTabBarController {
- @objc func refreshUpdateView() {
- if TSAppUpdateManager.isNeedUpdate,TSAppUpdateManager.isDisplayedUpdateAlert == false{
- kMainAfter(1.0) {
- let updateAlertVC = TSAppUpdateAlertVC()
- updateAlertVC.modalPresentationStyle = .overFullScreen
- updateAlertVC.modalTransitionStyle = .crossDissolve
- self.present(updateAlertVC, animated: true,completion: {
- TSAppUpdateManager.isDisplayedUpdateAlert = true
- })
- }
- }
- }
- }
- //关闭订阅页面,弹出优惠订阅
- extension TSTabBarController {
- @objc func refreshPurchasePromotional() {
- if TSPurchasePromotionalVC.showGift,kPurchaseCountDownTime.isNeedCheck == true{
- kMainAfter(0.3) {
- let updateAlertVC = TSPurchasePromotionalVC(isAnimation: true)
- updateAlertVC.modalPresentationStyle = .overFullScreen
- updateAlertVC.modalTransitionStyle = .crossDissolve
- self.present(updateAlertVC, animated: true,completion: {
- kPurchaseCountDownTime.isNeedCheck = false
- })
- }
- }
- }
- }
- //private let animatedIndex = 1
- //extension TSTabBarController {
- // func addAnimatedImageView(){
- //
- // let index = 1
- // if let tabBarItems = tabBar.items,tabBarItems.count > index {
- // let tabBarButtons = tabBar.subviews.filter({ $0.isKind(of: NSClassFromString("UITabBarButton")!) })
- // if tabBarButtons.count > index {
- // let thirdTabButton = tabBarButtons[index]
- // thirdTabButton.addSubview(animatedImageView)
- // animatedImageView.snp.makeConstraints { make in
- // make.width.equalTo(13.0)
- // make.height.equalTo(22.0)
- // make.centerX.equalToSuperview().offset(11)
- // make.top.equalTo(6)
- // }
- // kDelayMainShort {
- // self.animatedImageView.startAnimating()
- // }
- //
- // }
- // }
- // }
- //}
- extension TSTabBarController:UITabBarControllerDelegate {
- func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
- // if tabBarController.selectedIndex == animatedIndex {
- // self.animatedImageView.isHidden = true
- // }else{
- // self.animatedImageView.isHidden = false
- // }
- }
- }
- protocol TSTabBarControllerProtocol {
- func tabBarDoubleTap(_ tabbar: UITabBarController)
- }
- extension TSTabBarController {
- func setupDoubleTapToScrollTop() {
- let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
- doubleTap.numberOfTapsRequired = 2
- doubleTap.delaysTouchesBegan = false // 默认是true,会延迟单击响应
- doubleTap.delaysTouchesEnded = false
- tabBar.addGestureRecognizer(doubleTap)
- }
- @objc private func handleDoubleTap(_ gesture: UITapGestureRecognizer) {
- guard gesture.state == .ended else { return }
- guard let vc = selectedViewController else { return }
-
- if let vc = vc as? TSTabBarControllerProtocol{
- vc.tabBarDoubleTap(self)
- }else if let nav = vc as? UINavigationController {
- if let topVC = nav.topViewController {
-
- if let vc = topVC as? TSTabBarControllerProtocol{
- vc.tabBarDoubleTap(self)
- }
- }
- }
- }
- }
|