CGRect+ManualLayout.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // CGRect+ManualLayout.swift
  3. // ManualLayout
  4. //
  5. // Created by Baris Sencan on 30/03/15.
  6. // Copyright (c) 2015 Baris Sencan. All rights reserved.
  7. //
  8. import UIKit
  9. public extension CGRect {
  10. // MARK: - Position
  11. public var x: CGFloat {
  12. get {
  13. return origin.x
  14. }
  15. set {
  16. origin.x = snapToPixel(pointCoordinate: newValue)
  17. }
  18. }
  19. public var y: CGFloat {
  20. get {
  21. return origin.y
  22. }
  23. set {
  24. origin.y = snapToPixel(pointCoordinate: newValue)
  25. }
  26. }
  27. public var center: CGPoint {
  28. get {
  29. return CGPoint(x: centerX, y: centerY)
  30. }
  31. set {
  32. centerX = newValue.x
  33. centerY = newValue.y
  34. }
  35. }
  36. public var centerX: CGFloat {
  37. get {
  38. return origin.x + size.width / 2
  39. }
  40. set {
  41. x = newValue - size.width / 2
  42. }
  43. }
  44. public var centerY: CGFloat {
  45. get {
  46. return origin.y + size.height / 2
  47. }
  48. set {
  49. y = newValue - size.height / 2
  50. }
  51. }
  52. // MARK: - Edges
  53. public var top: CGFloat {
  54. get {
  55. return origin.y
  56. }
  57. set {
  58. y = newValue
  59. }
  60. }
  61. public var right: CGFloat {
  62. get {
  63. return origin.x + size.width
  64. }
  65. set {
  66. x = newValue - size.width
  67. }
  68. }
  69. public var bottom: CGFloat {
  70. get {
  71. return origin.y + size.height
  72. }
  73. set {
  74. y = newValue - size.height
  75. }
  76. }
  77. public var left: CGFloat {
  78. get {
  79. return origin.x
  80. }
  81. set {
  82. x = newValue
  83. }
  84. }
  85. // MARK: - Alternative Edges
  86. public var top2: CGFloat {
  87. get {
  88. return origin.y
  89. }
  90. set {
  91. if newValue <= bottom {
  92. size.height += snapToPixel(pointCoordinate: top - newValue)
  93. y = newValue
  94. } else {
  95. // Swap top with bottom.
  96. let newTop = bottom
  97. size.height = snapToPixel(pointCoordinate: newValue - newTop)
  98. y = newTop
  99. }
  100. }
  101. }
  102. public var right2: CGFloat {
  103. get {
  104. return origin.x + size.width
  105. }
  106. set {
  107. if newValue >= left {
  108. size.width += snapToPixel(pointCoordinate: newValue - right)
  109. } else {
  110. // Swap left with right.
  111. let newRight = left
  112. size.width = snapToPixel(pointCoordinate: newRight - newValue)
  113. x = newValue
  114. }
  115. }
  116. }
  117. public var bottom2: CGFloat {
  118. get {
  119. return origin.y + size.height
  120. }
  121. set {
  122. if newValue >= top {
  123. size.height += snapToPixel(pointCoordinate: newValue - bottom)
  124. } else {
  125. // Swap bottom with top.
  126. let newBottom = top
  127. size.height = snapToPixel(pointCoordinate: newBottom - newValue)
  128. y = newValue
  129. }
  130. }
  131. }
  132. public var left2: CGFloat {
  133. get {
  134. return origin.x
  135. }
  136. set {
  137. if newValue <= right {
  138. size.width += snapToPixel(pointCoordinate: left - newValue)
  139. x = newValue
  140. } else {
  141. // Swap right with left.
  142. let newLeft = right
  143. size.width = snapToPixel(pointCoordinate: newValue - newLeft)
  144. x = newLeft
  145. }
  146. }
  147. }
  148. }