LNMainTabBar.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // LNMainTabBar.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/26.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNMainTabBar: UITabBar {
  11. private let gradientLayer = CAGradientLayer()
  12. private let container = UIView()
  13. private var tabBarButtons: [UIView] = []
  14. override init(frame: CGRect) {
  15. super.init(frame: frame)
  16. clipsToBounds = false
  17. gradientLayer.colors = [
  18. UIColor.init(hex: "#F1F2F500").cgColor,
  19. UIColor.white.cgColor
  20. ]
  21. gradientLayer.locations = [0, 1]
  22. gradientLayer.startPoint = CGPoint(x: 0, y: 0)
  23. gradientLayer.endPoint = CGPoint(x: 0, y: 1)
  24. layer.addSublayer(gradientLayer)
  25. container.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
  26. container.layer.shadowOffset = .init(width: 0, height: 2)
  27. container.layer.shadowRadius = 6
  28. container.layer.shadowOpacity = 0.8
  29. container.layer.masksToBounds = false
  30. addSubview(container)
  31. container.snp.makeConstraints { make in
  32. make.horizontalEdges.equalToSuperview().inset(42)
  33. make.bottom.equalToSuperview().offset(-36)
  34. make.height.equalTo(60)
  35. }
  36. let blurEffect = UIBlurEffect(style: .regular)
  37. let blurView = UIVisualEffectView(effect: blurEffect)
  38. blurView.layer.cornerRadius = 30
  39. blurView.clipsToBounds = true
  40. container.addSubview(blurView)
  41. blurView.snp.makeConstraints { make in
  42. make.edges.equalToSuperview()
  43. }
  44. let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
  45. let vibrancyView = UIVisualEffectView(effect: vibrancyEffect)
  46. blurView.contentView.addSubview(vibrancyView)
  47. }
  48. func setupViews() {
  49. tabBarButtons = subviews.filter { NSStringFromClass($0.classForCoder) == "UITabBarButton" }
  50. for (index, button) in tabBarButtons.enumerated() {
  51. button.removeFromSuperview()
  52. container.addSubview(button)
  53. button.snp.makeConstraints { make in
  54. make.centerX.equalToSuperview().multipliedBy(CGFloat(2 * index + 1) / CGFloat(tabBarButtons.count))
  55. make.height.equalToSuperview().offset(-6)
  56. make.width.equalToSuperview().multipliedBy(1.0 / CGFloat(tabBarButtons.count)).offset(-6)
  57. }
  58. if let ic = button.subviews.first(where: { $0 is UIImageView }) {
  59. ic.snp.makeConstraints { make in
  60. make.centerY.equalTo(container)
  61. }
  62. } else {
  63. button.snp.makeConstraints { make in
  64. make.centerY.equalToSuperview()
  65. }
  66. }
  67. }
  68. }
  69. override func layoutSubviews() {
  70. super.layoutSubviews()
  71. gradientLayer.frame = .init(x: 0, y: bounds.height - 96, width: bounds.width, height: 96)
  72. }
  73. // UITabBar 默认会进行坐标转换,导致点击会有偏差响应
  74. // 这里重新进行点击判断,进行响应修复
  75. override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
  76. if !isHidden, let button = tabBarButtons.first(where: {
  77. let frame = $0.convert($0.bounds, to: self)
  78. return frame.contains(point)
  79. }) {
  80. return button
  81. }
  82. return frame.contains(point) ? self : nil
  83. }
  84. required init?(coder: NSCoder) {
  85. fatalError("init(coder:) has not been implemented")
  86. }
  87. }