Last active
November 23, 2023 06:15
-
-
Save cotkjaer/9b5af14a473e7c20fec1b0e965fe302b to your computer and use it in GitHub Desktop.
Revisions
-
cotkjaer revised this gist
Mar 18, 2017 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,17 @@ extension CGRect { /** Creates a rectangle with the given center and dimensions - parameter center: The center of the new rectangle - parameter size: The dimensions of the new rectangle */ init(center: CGPoint, size: CGSize) { self.init(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height) } /** the coordinates of this rectangles center */ var center: CGPoint { get { return CGPoint(x: centerX, y: centerY) } set { centerX = newValue.x; centerY = newValue.y } @@ -21,7 +21,7 @@ public extension CGRect - note: Acts as a settable midX - returns: The x-coordinate of the center */ var centerX: CGFloat { get { return midX } set { origin.x = newValue - width * 0.5 } @@ -31,7 +31,7 @@ public extension CGRect - note: Acts as a settable midY - returns: The y-coordinate of the center */ var centerY: CGFloat { get { return midY } set { origin.y = newValue - height * 0.5 } @@ -43,7 +43,7 @@ public extension CGRect - parameter center: The new center, ignored if nil - returns: A new rectangle with the same size and a new center */ func with(center: CGPoint?) -> CGRect { return CGRect(center: center ?? self.center, size: size) } @@ -52,7 +52,7 @@ public extension CGRect - parameter centerX: The new center-x, ignored if nil - returns: A new rectangle with the same size and a new center */ func with(centerX: CGFloat?) -> CGRect { return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY), size: size) } @@ -61,7 +61,7 @@ public extension CGRect - parameter centerY: The new center-y, ignored if nil - returns: A new rectangle with the same size and a new center */ func with(centerY: CGFloat?) -> CGRect { return CGRect(center: CGPoint(x: centerX, y: centerY ?? self.centerY), size: size) } @@ -71,7 +71,7 @@ public extension CGRect - parameter centerY: The new center-y, ignored if nil - returns: A new rectangle with the same size and a new center */ func with(centerX: CGFloat?, centerY: CGFloat?) -> CGRect { return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY ?? self.centerY), size: size) } -
cotkjaer created this gist
Mar 18, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ public extension CGRect { /** Creates a rectangle with the given center and dimensions - parameter center: The center of the new rectangle - parameter size: The dimensions of the new rectangle */ public init(center: CGPoint, size: CGSize) { self.init(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height) } /** the coordinates of this rectangles center */ public var center: CGPoint { get { return CGPoint(x: centerX, y: centerY) } set { centerX = newValue.x; centerY = newValue.y } } /** the x-coordinate of this rectangles center - note: Acts as a settable midX - returns: The x-coordinate of the center */ public var centerX: CGFloat { get { return midX } set { origin.x = newValue - width * 0.5 } } /** the y-coordinate of this rectangles center - note: Acts as a settable midY - returns: The y-coordinate of the center */ public var centerY: CGFloat { get { return midY } set { origin.y = newValue - height * 0.5 } } // MARK: - "with" convenience functions /** Same-sized rectangle with a new center - parameter center: The new center, ignored if nil - returns: A new rectangle with the same size and a new center */ public func with(center: CGPoint?) -> CGRect { return CGRect(center: center ?? self.center, size: size) } /** Same-sized rectangle with a new center-x - parameter centerX: The new center-x, ignored if nil - returns: A new rectangle with the same size and a new center */ public func with(centerX: CGFloat?) -> CGRect { return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY), size: size) } /** Same-sized rectangle with a new center-y - parameter centerY: The new center-y, ignored if nil - returns: A new rectangle with the same size and a new center */ public func with(centerY: CGFloat?) -> CGRect { return CGRect(center: CGPoint(x: centerX, y: centerY ?? self.centerY), size: size) } /** Same-sized rectangle with a new center-x and center-y - parameter centerX: The new center-x, ignored if nil - parameter centerY: The new center-y, ignored if nil - returns: A new rectangle with the same size and a new center */ public func with(centerX: CGFloat?, centerY: CGFloat?) -> CGRect { return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY ?? self.centerY), size: size) } }