Day_Count_Memory_Days/anniversary_Project/Main/AV_RootTabbarVC.swift
2024-07-15 11:52:15 +08:00

91 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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
}
}