LNBottomSheetMenu.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // LNBottomSheetMenu.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/12.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNBottomSheetMenu: LNPopupView {
  11. let stackView = UIStackView()
  12. override init(frame: CGRect) {
  13. super.init(frame: frame)
  14. setupViews()
  15. }
  16. func update(_ menus: [String], handler: @escaping (Int, String) -> Void) {
  17. guard !menus.isEmpty else { return }
  18. stackView.arrangedSubviews.forEach {
  19. stackView.removeArrangedSubview($0)
  20. $0.removeFromSuperview()
  21. }
  22. var itemViews: [UIView] = []
  23. for (index, menu) in menus.enumerated() {
  24. let view = UIView()
  25. view.snp.makeConstraints { make in
  26. make.height.equalTo(50)
  27. }
  28. view.onTap { [weak self] in
  29. guard let self else { return }
  30. handler(index, menu)
  31. dismiss()
  32. }
  33. let titleLabel = UILabel()
  34. titleLabel.text = menu
  35. titleLabel.font = .heading_h4
  36. titleLabel.textColor = .text_5
  37. view.addSubview(titleLabel)
  38. titleLabel.snp.makeConstraints { make in
  39. make.center.equalToSuperview()
  40. }
  41. itemViews.append(view)
  42. }
  43. for index in (1..<itemViews.count).reversed() {
  44. itemViews.insert(buildLine(), at: index)
  45. }
  46. itemViews.forEach {
  47. stackView.addArrangedSubview($0)
  48. }
  49. }
  50. required init?(coder: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. }
  54. extension LNBottomSheetMenu {
  55. private func setupViews() {
  56. stackView.axis = .vertical
  57. stackView.spacing = 0
  58. container.addSubview(stackView)
  59. stackView.snp.makeConstraints { make in
  60. make.horizontalEdges.equalToSuperview()
  61. make.top.equalToSuperview()
  62. make.bottom.equalToSuperview().offset(commonBottomInset)
  63. }
  64. }
  65. private func buildLine() -> UIView {
  66. let container = UIView()
  67. let line = UIView()
  68. line.backgroundColor = .fill_2
  69. container.addSubview(line)
  70. line.snp.makeConstraints { make in
  71. make.verticalEdges.equalToSuperview()
  72. make.height.equalTo(1)
  73. make.horizontalEdges.equalToSuperview().inset(16)
  74. }
  75. return container
  76. }
  77. }