// // LNMainTabBar.swift // Lanu // // Created by OneeChan on 2025/12/26. // import Foundation import UIKit import SnapKit class LNMainTabBar: UITabBar { private let gradientLayer = CAGradientLayer() private let container = UIView() private var tabBarButtons: [UIView] = [] override init(frame: CGRect) { super.init(frame: frame) clipsToBounds = false gradientLayer.colors = [ UIColor.init(hex: "#F1F2F500").cgColor, UIColor.white.cgColor ] gradientLayer.locations = [0, 1] gradientLayer.startPoint = CGPoint(x: 0, y: 0) gradientLayer.endPoint = CGPoint(x: 0, y: 1) layer.addSublayer(gradientLayer) container.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor container.layer.shadowOffset = .init(width: 0, height: 2) container.layer.shadowRadius = 6 container.layer.shadowOpacity = 0.8 container.layer.masksToBounds = false addSubview(container) container.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview().inset(42) make.bottom.equalToSuperview().offset(-36) make.height.equalTo(60) } let blurEffect = UIBlurEffect(style: .regular) let blurView = UIVisualEffectView(effect: blurEffect) blurView.layer.cornerRadius = 30 blurView.clipsToBounds = true container.addSubview(blurView) blurView.snp.makeConstraints { make in make.edges.equalToSuperview() } let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect) let vibrancyView = UIVisualEffectView(effect: vibrancyEffect) blurView.contentView.addSubview(vibrancyView) } func setupViews() { tabBarButtons = subviews.filter { NSStringFromClass($0.classForCoder) == "UITabBarButton" } for (index, button) in tabBarButtons.enumerated() { button.removeFromSuperview() container.addSubview(button) button.snp.makeConstraints { make in make.centerX.equalToSuperview().multipliedBy(CGFloat(2 * index + 1) / CGFloat(tabBarButtons.count)) make.height.equalToSuperview().offset(-6) make.width.equalToSuperview().multipliedBy(1.0 / CGFloat(tabBarButtons.count)).offset(-6) } if let ic = button.subviews.first(where: { $0 is UIImageView }) { ic.snp.makeConstraints { make in make.centerY.equalTo(container) } } else { button.snp.makeConstraints { make in make.centerY.equalToSuperview() } } } } override func layoutSubviews() { super.layoutSubviews() gradientLayer.frame = .init(x: 0, y: bounds.height - 96, width: bounds.width, height: 96) } // UITabBar 默认会进行坐标转换,导致点击会有偏差响应 // 这里重新进行点击判断,进行响应修复 override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { if !isHidden, let button = tabBarButtons.first(where: { let frame = $0.convert($0.bounds, to: self) return frame.contains(point) }) { return button } return frame.contains(point) ? self : nil } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }