UserNameInputView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // ChangeUserNameView.swift
  3. // TUIRoomKit
  4. //
  5. // Created by janejntang on 2024/9/10.
  6. //
  7. import UIKit
  8. import SnapKit
  9. protocol UserNameInputViewResponder: AnyObject {
  10. func changeUserName(name: String)
  11. func onEndEditing()
  12. }
  13. class UserNameInputView: UIView {
  14. weak var viewResponder: UserNameInputViewResponder?
  15. private let maxInputBytes = 32
  16. var userName: String?
  17. private let topView: UIView = {
  18. let view = UIView()
  19. view.backgroundColor = UIColor(0x22262E)
  20. return view
  21. }()
  22. private let backgroundView: UIVisualEffectView = {
  23. let blurEffect = UIBlurEffect(style: .dark)
  24. let view = UIVisualEffectView(effect: blurEffect)
  25. view.frame = UIScreen.main.bounds
  26. return view
  27. }()
  28. private lazy var inputTextView: UITextView = {
  29. let view = UITextView(frame: .zero)
  30. view.font = UIFont.systemFont(ofSize: 17.5, weight: .regular)
  31. view.returnKeyType = .done
  32. view.layer.cornerRadius = view.sizeThatFits(.zero).height / 2
  33. view.layer.masksToBounds = true
  34. view.textColor = UIColor(0xD5E0F2).withAlphaComponent(0.6)
  35. view.backgroundColor = UIColor(0x4F586B).withAlphaComponent(0.3)
  36. view.text = userName
  37. view.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
  38. return view
  39. }()
  40. private var isViewReady: Bool = false
  41. override func didMoveToWindow() {
  42. super.didMoveToWindow()
  43. guard !isViewReady else { return }
  44. isViewReady = true
  45. constructViewHierarchy()
  46. activateConstraints()
  47. bindInteraction()
  48. }
  49. private func constructViewHierarchy() {
  50. addSubview(backgroundView)
  51. topView.addSubview(inputTextView)
  52. addSubview(topView)
  53. }
  54. private func activateConstraints() {
  55. backgroundView.snp.makeConstraints { make in
  56. make.edges.equalToSuperview()
  57. }
  58. topView.snp.makeConstraints { make in
  59. make.leading.trailing.equalToSuperview()
  60. make.height.equalTo(60.scale375Height())
  61. make.bottom.equalToSuperview()
  62. }
  63. inputTextView.snp.makeConstraints { make in
  64. make.leading.equalToSuperview().offset(16.scale375())
  65. make.trailing.equalToSuperview().offset(-16.scale375())
  66. make.height.equalTo(36.scale375Height())
  67. make.center.equalToSuperview()
  68. }
  69. }
  70. private func bindInteraction() {
  71. isHidden = true
  72. inputTextView.delegate = self
  73. NotificationCenter.default.addObserver(self,
  74. selector: #selector(keyboardWillShow(notification: )),
  75. name: UIResponder.keyboardWillShowNotification,
  76. object: nil)
  77. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  78. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(endEdit))
  79. backgroundView.addGestureRecognizer(tapGesture)
  80. }
  81. func showInputView(userName: String?) {
  82. inputTextView.text = userName
  83. isHidden = false
  84. inputTextView.becomeFirstResponder()
  85. }
  86. @objc func endEdit() {
  87. inputTextView.endEditing(true)
  88. viewResponder?.onEndEditing()
  89. }
  90. func hideInputView() {
  91. inputTextView.endEditing(true)
  92. isHidden = true
  93. }
  94. @objc func keyboardWillShow(notification: NSNotification) {
  95. guard let keyboardRect: CGRect = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
  96. let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
  97. else {
  98. return
  99. }
  100. UIView.animate(withDuration: duration, delay: 0, options: .curveEaseInOut) { [weak self] in
  101. guard let self = self else { return }
  102. self.topView.transform = CGAffineTransform(translationX: 0, y: -keyboardRect.height)
  103. }
  104. }
  105. @objc func keyboardWillHide(notification: NSNotification) {
  106. if let keyboardRect: CGRect = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
  107. let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double {
  108. UIView.animate(withDuration: duration, delay: 0, options: .curveEaseInOut) { [weak self] in
  109. guard let self = self else { return }
  110. self.topView.transform = CGAffineTransform(translationX: 0, y: keyboardRect.height)
  111. } completion: { [weak self] _ in
  112. guard let self = self else { return }
  113. self.isHidden = true
  114. }
  115. } else {
  116. isHidden = true
  117. }
  118. }
  119. deinit {
  120. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
  121. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  122. }
  123. }
  124. extension UserNameInputView: UITextViewDelegate {
  125. func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
  126. if text == "\n" {
  127. if !textView.text.isEmpty {
  128. viewResponder?.changeUserName(name: textView.text)
  129. }
  130. endEdit()
  131. return false
  132. }
  133. let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
  134. if let newTextData = newText.data(using: .utf8), newTextData.count <= maxInputBytes {
  135. return true
  136. } else {
  137. return false
  138. }
  139. }
  140. }