MOPopupViewProtocol.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // MOPopupViewProtocol.swift
  3. // MiMoLive
  4. //
  5. // Created by OneeChan on 2025/9/25.
  6. //
  7. import Foundation
  8. import UIKit
  9. enum PopupViewHeight {
  10. case percent(CGFloat)
  11. case height(CGFloat)
  12. }
  13. protocol MOPopupViewProtocol: UIView {
  14. var container: UIView { get }
  15. var containerHeight: PopupViewHeight { get }
  16. }
  17. extension MOPopupViewProtocol {
  18. func showHIn(_ targetView: UIView) {
  19. if superview != nil, superview != targetView {
  20. removeFromSuperview()
  21. }
  22. targetView.addSubview(self)
  23. self.snp.makeConstraints { make in
  24. make.edges.equalToSuperview()
  25. }
  26. if container.superview == nil {
  27. self.addSubview(container)
  28. }
  29. container.snp.remakeConstraints { make in
  30. make.leading.equalTo(self.snp.trailing)
  31. make.width.equalToSuperview()
  32. make.bottom.equalToSuperview()
  33. if case .percent(let percent) = containerHeight {
  34. make.height.equalToSuperview().multipliedBy(percent)
  35. } else if case .height(let height) = containerHeight {
  36. make.height.equalTo(height)
  37. }
  38. }
  39. layoutIfNeeded()
  40. container.snp.remakeConstraints { make in
  41. make.leading.equalToSuperview()
  42. make.width.equalToSuperview()
  43. make.bottom.equalToSuperview()
  44. if case .percent(let percent) = containerHeight {
  45. make.height.equalToSuperview().multipliedBy(percent)
  46. } else if case .height(let height) = containerHeight {
  47. make.height.equalTo(height)
  48. }
  49. }
  50. UIView.animate(withDuration: 0.2) {
  51. self.layoutIfNeeded()
  52. }
  53. NotificationCenter.default.post(name: NSNotification.Name("MOShowLivePagesVCCannotScroll"), object: nil)
  54. }
  55. func dismissH() {
  56. container.snp.remakeConstraints { make in
  57. make.leading.equalTo(self.snp.trailing)
  58. make.width.equalToSuperview()
  59. make.bottom.equalToSuperview()
  60. if case .percent(let percent) = containerHeight {
  61. make.height.equalToSuperview().multipliedBy(percent)
  62. } else if case .height(let height) = containerHeight {
  63. make.height.equalTo(height)
  64. }
  65. }
  66. UIView.animate(withDuration: 0.2) {
  67. self.layoutIfNeeded()
  68. } completion: { _ in
  69. self.removeFromSuperview()
  70. }
  71. NotificationCenter.default.post(name: NSNotification.Name("MOShowLivePagesVCCanScroll"), object: nil)
  72. }
  73. func showVIn(_ targetView: UIView) {
  74. if superview != nil, superview != targetView {
  75. removeFromSuperview()
  76. }
  77. targetView.addSubview(self)
  78. self.snp.makeConstraints { make in
  79. make.edges.equalToSuperview()
  80. }
  81. if container.superview == nil {
  82. self.addSubview(container)
  83. }
  84. container.snp.remakeConstraints { make in
  85. make.leading.trailing.equalToSuperview()
  86. make.top.equalTo(self.snp.bottom)
  87. if case .percent(let percent) = containerHeight {
  88. make.height.equalToSuperview().multipliedBy(percent)
  89. } else if case .height(let height) = containerHeight {
  90. make.height.equalTo(height)
  91. }
  92. }
  93. layoutIfNeeded()
  94. container.snp.remakeConstraints { make in
  95. make.leading.trailing.bottom.equalToSuperview()
  96. if case .percent(let percent) = containerHeight {
  97. make.height.equalToSuperview().multipliedBy(percent)
  98. } else if case .height(let height) = containerHeight {
  99. make.height.equalTo(height)
  100. }
  101. }
  102. UIView.animate(withDuration: 0.2) {
  103. self.layoutIfNeeded()
  104. }
  105. NotificationCenter.default.post(name: NSNotification.Name("MOShowLivePagesVCCannotScroll"), object: nil)
  106. }
  107. func dismissV() {
  108. container.snp.remakeConstraints { make in
  109. make.leading.trailing.equalToSuperview()
  110. make.top.equalTo(self.snp.bottom)
  111. if case .percent(let percent) = containerHeight {
  112. make.height.equalToSuperview().multipliedBy(percent)
  113. } else if case .height(let height) = containerHeight {
  114. make.height.equalTo(height)
  115. }
  116. }
  117. UIView.animate(withDuration: 0.2) {
  118. self.layoutIfNeeded()
  119. } completion: { _ in
  120. self.removeFromSuperview()
  121. }
  122. NotificationCenter.default.post(name: NSNotification.Name("MOShowLivePagesVCCanScroll"), object: nil)
  123. }
  124. }