// // AV_RootTabbarVC.swift // anniversary_Project // // Created by 忆海16 on 2024/4/11. // import UIKit class AV_RootTabbarVC: UITabBarController { private var middleButton: UIButton! override func viewDidLoad() { super.viewDidLoad() let firstViewController = AV_HomeVC() firstViewController.view.backgroundColor = .white firstViewController.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "home_n"), selectedImage: UIImage(named: "home_s")) let secondViewController = AV_MineVC() secondViewController.view.backgroundColor = .white secondViewController.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "bir_n"), selectedImage: UIImage(named: "bir_s")) secondViewController.tabBarItem.imageInsets = UIEdgeInsets.init(top: -3, left: 0, bottom: -3, right: 0) viewControllers = [firstViewController, secondViewController] setupMiddleButton() self.delegate = self let tabBarBackgroundImage = UIImage(named: "tabbar") tabBar.backgroundImage = tabBarBackgroundImage } private func setupMiddleButton() { middleButton = UIButton(type: .custom) middleButton.setBackgroundImage(UIImage(named: "add"), for: .normal) middleButton.adjustsImageWhenHighlighted = false middleButton.translatesAutoresizingMaskIntoConstraints = false middleButton.addTarget(self, action: #selector(middleButtonTapped), for: .touchUpInside) view.addSubview(middleButton) NSLayoutConstraint.activate([ middleButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), middleButton.bottomAnchor.constraint(equalTo: tabBar.topAnchor, constant: 70), // 调整按钮位置 middleButton.widthAnchor.constraint(equalToConstant: 100), // 调整按钮大小 middleButton.heightAnchor.constraint(equalToConstant: 100) ]) } @objc private func middleButtonTapped() { // 处理中间按钮被点击的事件 // 例如:present一个新的视图控制器,用于执行中间按钮的操作 if let keyWindow = UIApplication.shared.keyWindow, let rootViewController = keyWindow.rootViewController { let viewControllerToPresent = AV_AddVC() viewControllerToPresent.modalPresentationStyle = .fullScreen rootViewController.present(viewControllerToPresent, animated: true, completion: nil) } } } extension AV_RootTabbarVC: UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { if let tabBarItems = tabBarController.tabBar.items, let selectedViewControllerIndex = tabBarController.viewControllers?.firstIndex(of: viewController) { let selectedItem = tabBarItems[selectedViewControllerIndex] // Apply scale animation only to the selected item let scaleAnimation = CABasicAnimation(keyPath: "transform.scale") scaleAnimation.duration = 0.2 scaleAnimation.fromValue = 0.9 scaleAnimation.toValue = 1.1 scaleAnimation.autoreverses = true scaleAnimation.repeatCount = 1 // Apply animation to the selected item's view if let selectedView = selectedItem.value(forKey: "view") as? UIView { selectedView.layer.add(scaleAnimation, forKey: nil) } } return true } }