UIViewExtension.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // UIViewExtension.swift
  3. // BSText
  4. //
  5. // Created by BlueSky on 2018/10/22.
  6. // Copyright © 2019 GeekBruce. All rights reserved.
  7. //
  8. import UIKit
  9. public extension UIView {
  10. @objc var bs_viewController: UIViewController? {
  11. get {
  12. var view: UIView? = self
  13. while (view != nil) {
  14. let nextResponder = view?.next
  15. if (nextResponder is UIViewController) {
  16. return nextResponder as? UIViewController
  17. }
  18. view = view?.superview
  19. }
  20. return nil
  21. }
  22. }
  23. @objc var bs_visibleAlpha: CGFloat {
  24. get {
  25. if (self is UIWindow) {
  26. if isHidden {
  27. return 0
  28. }
  29. return self.alpha
  30. }
  31. if !(window != nil) {
  32. return 0
  33. }
  34. var alpha: CGFloat = 1
  35. var v: UIView? = self
  36. while v != nil {
  37. if v!.isHidden {
  38. alpha = 0
  39. break
  40. }
  41. alpha *= v!.alpha
  42. v = v!.superview
  43. }
  44. return alpha
  45. }
  46. }
  47. @objc func bs_convertPoint(_ point: CGPoint, toViewOrWindow view: UIView?) -> CGPoint {
  48. var point = point
  49. if view == nil {
  50. if (self is UIWindow) {
  51. return (self as? UIWindow)?.convert(point, to: nil) ?? CGPoint.zero
  52. } else {
  53. return convert(point, to: nil)
  54. }
  55. }
  56. let from: UIWindow? = (self is UIWindow) ? (self as! UIWindow) : window
  57. let to = (view is UIWindow) ? (view as? UIWindow) : view?.window
  58. if (from == nil || to == nil) || (from == to) {
  59. return convert(point, to: view)
  60. }
  61. point = convert(point, to: from)
  62. point = to?.convert(point, from: from) ?? CGPoint.zero
  63. point = view?.convert(point, from: to) ?? CGPoint.zero
  64. return point
  65. }
  66. @objc func bs_convertPoint(_ point: CGPoint, fromViewOrWindow view: UIView?) -> CGPoint {
  67. var point = point
  68. if view == nil {
  69. if (self is UIWindow) {
  70. return (self as? UIWindow)?.convert(point, from: nil) ?? CGPoint.zero
  71. } else {
  72. return convert(point, from: nil)
  73. }
  74. }
  75. let from = (view is UIWindow) ? (view as? UIWindow) : view?.window
  76. let to: UIWindow? = (self is UIWindow) ? (self as! UIWindow) : window
  77. if (from == nil || to == nil) || (from == to) {
  78. return convert(point, from: view)
  79. }
  80. point = from?.convert(point, from: view) ?? CGPoint.zero
  81. point = to?.convert(point, from: from) ?? CGPoint.zero
  82. point = convert(point, from: to)
  83. return point
  84. }
  85. @objc func bs_convertRect(_ rect: CGRect, toViewOrWindow view: UIView?) -> CGRect {
  86. var rect = rect
  87. if view == nil {
  88. if (self is UIWindow) {
  89. return (self as? UIWindow)?.convert(rect, to: nil) ?? CGRect.zero
  90. } else {
  91. return convert(rect, to: nil)
  92. }
  93. }
  94. let from: UIWindow? = (self is UIWindow) ? (self as! UIWindow) : window
  95. let to = (view is UIWindow) ? (view as? UIWindow) : view?.window
  96. if from == nil || to == nil {
  97. return convert(rect, to: view)
  98. }
  99. if from == to {
  100. return convert(rect, to: view)
  101. }
  102. rect = convert(rect, to: from)
  103. rect = to?.convert(rect, from: from) ?? CGRect.zero
  104. rect = view?.convert(rect, from: to) ?? CGRect.zero
  105. return rect
  106. }
  107. @objc func bs_convertRect(_ rect: CGRect, fromViewOrWindow view: UIView?) -> CGRect {
  108. var rect = rect
  109. if view == nil {
  110. if (self is UIWindow) {
  111. return (self as? UIWindow)?.convert(rect, from: nil) ?? CGRect.zero
  112. } else {
  113. return convert(rect, from: nil)
  114. }
  115. }
  116. let from = (view is UIWindow) ? (view as? UIWindow) : view?.window
  117. let to: UIWindow? = (self is UIWindow) ? (self as! UIWindow) : window
  118. if (from == nil || to == nil) || (from == to) {
  119. return convert(rect, from: view)
  120. }
  121. rect = from?.convert(rect, from: view) ?? CGRect.zero
  122. rect = to?.convert(rect, from: from) ?? CGRect.zero
  123. rect = convert(rect, from: to)
  124. return rect
  125. }
  126. }