123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // 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]) { result in
- switch result {
- case .success(let value):
- print("GIF 加载成功: \(value.source.url?.absoluteString ?? "")")
- case .failure(let error):
- print("GIF 加载失败: \(error.localizedDescription)")
- }
- }
- }
- return animatedImageView
- }()
-
- private var viewControllerArray: [String] = []
- private var titleArray: [String] = []
- private var selectedImageArray: [String] = []
- private var unselectedImageArray: [String] = []
-
- private var interceptVC: UINavigationController = UINavigationController()
-
- override func viewDidLoad() {
- super.viewDidLoad()
- createUI()
- setUpData()
-
- addAnimatedImageView()
- }
- @objc private func setUpData() {
- viewControllerArray = ["TSPTPInputVC","TSAILIstVC","TSTTPInputVC","TSGenerateHistoryVC","TSSetingVC"]
- titleArray = ["Photo","AI Store","Text","Creations","Setting"]
- selectedImageArray = [
- "tabbar_select_pic",
- "tabbar_select_ailist",
- "tabbar_select_text",
- "tabbar_select_aichat",
- "tabbar_select_setting"
- ]
- unselectedImageArray = [
- "tabbar_unSelect_pic",
- "tabbar_unSelect_ailist",
- "tabbar_unSelect_text",
- "tabbar_unSelect_aichat",
- "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
- interceptVC = tabArray.last!
- }
- 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
-
- }
- 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.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -8, right: 0) // 向下移动图标
- return tabBarItem
- }
- deinit {
- debugPrint("TSTabBarController deinit")
- NotificationCenter.default.removeObserver(self)
- }
-
- }
- 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()
- }
-
- }
- }
- }
-
- func changeSelectedIndex(index:Int){
- self.selectedIndex = index
- }
- }
- extension TSTabBarController:UITabBarControllerDelegate {
- // func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool{
- // if interceptVC == viewController{
- // kPresentModalVC(target: self, modelVC: TSWallpaperVC())
- // return false
- // }
- // return true
- // }
-
- // UITabBarControllerDelegate 方法
- func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
- if tabBarController.selectedIndex == animatedIndex {
- self.animatedImageView.isHidden = true
- }else{
- self.animatedImageView.isHidden = false
- }
- }
- }
|