UIView+Extension.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // UIView+Extension.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/11.
  6. //
  7. import Foundation
  8. import UIKit
  9. extension UIView {
  10. var navigationController: UINavigationController? {
  11. let vc = viewController
  12. if let nav = vc as? UINavigationController {
  13. return nav
  14. }
  15. return vc?.navigationController
  16. }
  17. var viewController: UIViewController? {
  18. var responder: UIResponder? = self
  19. while responder != nil {
  20. if let viewController = responder as? UIViewController {
  21. return viewController
  22. }
  23. responder = responder?.next
  24. }
  25. if let window = self as? UIWindow {
  26. return window.rootViewController
  27. } else if let window {
  28. return window.rootViewController
  29. }
  30. return nil
  31. }
  32. static var appKeyWindow: UIWindow? {
  33. let scenes = UIApplication.shared.connectedScenes
  34. if scenes.isEmpty { return nil }
  35. var activeScene = scenes.filter { $0.activationState == .foregroundActive }
  36. if activeScene.isEmpty {
  37. activeScene.insert(scenes.first!)
  38. }
  39. let windowScenes = scenes.compactMap { $0 as? UIWindowScene }
  40. if windowScenes.isEmpty { return nil }
  41. return windowScenes.first?.windows.filter { $0.isKeyWindow }.first
  42. }
  43. static var statusBarHeight: CGFloat = {
  44. guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
  45. else {
  46. return 0
  47. }
  48. return windowScene.statusBarManager?.statusBarFrame.height ?? 0
  49. }()
  50. static var navigationBarHeight: CGFloat = {
  51. guard let navigationController = UIView.appKeyWindow?.rootViewController as? UINavigationController else {
  52. return 44
  53. }
  54. return navigationController.navigationBar.bounds.height
  55. }()
  56. var safeBottomInset: CGFloat {
  57. Self.appKeyWindow?.safeAreaInsets.bottom ?? 0
  58. }
  59. var commonBottomInset: CGFloat {
  60. -safeBottomInset - 10
  61. }
  62. }
  63. // MARK: 点击响应
  64. extension UIView {
  65. private class BlockTapGestureRecognizer: UITapGestureRecognizer {
  66. private var actionBlock: (() -> Void)?
  67. init(action block: (() -> Void)?) {
  68. super.init(target: nil, action: nil)
  69. self.actionBlock = block
  70. // 设置目标和动作
  71. addTarget(self, action: #selector(handleTap(_:)))
  72. }
  73. @objc private func handleTap(_ gesture: BlockTapGestureRecognizer) {
  74. actionBlock?()
  75. }
  76. }
  77. func onTap(_ block: @escaping () -> Void) {
  78. let tap = BlockTapGestureRecognizer(action: block)
  79. addGestureRecognizer(tap)
  80. isUserInteractionEnabled = true
  81. }
  82. private class BlockLongPressGestureRecognizer: UILongPressGestureRecognizer {
  83. private var actionBlock: (() -> Void)?
  84. init(action block: (() -> Void)?) {
  85. super.init(target: nil, action: nil)
  86. self.actionBlock = block
  87. // 设置目标和动作
  88. addTarget(self, action: #selector(handleTap(_:)))
  89. }
  90. @objc private func handleTap(_ gesture: BlockTapGestureRecognizer) {
  91. actionBlock?()
  92. }
  93. }
  94. func onLongPress(_ block: @escaping () -> Void) {
  95. let tap = BlockLongPressGestureRecognizer(action: block)
  96. addGestureRecognizer(tap)
  97. }
  98. }