MOBubbleToastView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // MOBubbleToastView.swift
  3. // MiMoLive
  4. //
  5. // Created by OneeChan on 2025/10/17.
  6. //
  7. import Foundation
  8. import UIKit
  9. @objcMembers
  10. class MOBubbleToastView: UIView {
  11. private let title = UILabel()
  12. override init(frame: CGRect) {
  13. super.init(frame: frame)
  14. setupViews()
  15. }
  16. required init?(coder: NSCoder) {
  17. fatalError("init(coder:) has not been implemented")
  18. }
  19. func showAt(_ targetView: UIView, title: String) {
  20. guard let view = targetView.superview else { return }
  21. self.title.text = title
  22. view.addSubview(self)
  23. makeConstraints { make in
  24. make.centerX.equalTo(targetView)
  25. make.bottom.equalTo(targetView.sTop).offset(-7)
  26. }
  27. MODelayTask.perform(delay: 3) { [weak self] in
  28. guard let self else { return }
  29. removeFromSuperview()
  30. }
  31. }
  32. }
  33. extension MOBubbleToastView {
  34. private func setupViews() {
  35. let text = buildTextView()
  36. addSubview(text)
  37. text.makeConstraints { make in
  38. make.leading.top.trailing.equalToSuperview()
  39. }
  40. let arrow = buildArrow()
  41. addSubview(arrow)
  42. arrow.makeConstraints { make in
  43. make.centerX.equalToSuperview()
  44. make.bottom.equalToSuperview()
  45. make.top.equalTo(text.sBottom).offset(-1)
  46. }
  47. }
  48. private func buildTextView() -> UIView {
  49. let container = UIView()
  50. container.backgroundColor = .black.withAlphaComponent(0.8)
  51. container.layer.cornerRadius = 10
  52. title.textColor = .white
  53. title.font = .systemFont(ofSize: 13)
  54. container.addSubview(title)
  55. title.makeConstraints { make in
  56. make.edges.equalToSuperview().inset(UIEdgeInsets(top: 7, left: 12, bottom: 7, right: 12))
  57. }
  58. return container
  59. }
  60. private func buildArrow() -> UIView {
  61. let arrow = UIImageView()
  62. arrow.image = .init(named: "icon_guilde_arrow_down")?.withRenderingMode(.alwaysTemplate)
  63. arrow.tintColor = .black.withAlphaComponent(0.8)
  64. return arrow
  65. }
  66. }
  67. //import SwiftUI
  68. //
  69. //struct MOBubbleToastViewPreview: UIViewRepresentable {
  70. // func makeUIView(context: Context) -> some UIView {
  71. // let view = UIView()
  72. //
  73. // let button = UIButton()
  74. // button.backgroundColor = .black
  75. // view.addSubview(button)
  76. // button.makeConstraints { make in
  77. // make.center.equalToSuperview()
  78. // }
  79. //
  80. // let list = MOBubbleToastView()
  81. // list.showAt(button, title: "123123")
  82. //
  83. // return view
  84. // }
  85. //
  86. // func updateUIView(_ uiView: UIViewType, context: Context) { }
  87. //}
  88. //
  89. //#Preview {
  90. // MOBubbleToastViewPreview()
  91. //}