Last active
November 23, 2023 06:15
-
-
Save cotkjaer/9b5af14a473e7c20fec1b0e965fe302b to your computer and use it in GitHub Desktop.
Swift extensions to add "center" to CGRect
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 characters
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 } | |
} | |
/** the x-coordinate of this rectangles center | |
- 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 } | |
} | |
/** the y-coordinate of this rectangles center | |
- 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 } | |
} | |
// 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 | |
*/ | |
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 | |
*/ | |
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 | |
*/ | |
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 | |
*/ | |
func with(centerX: CGFloat?, centerY: CGFloat?) -> CGRect | |
{ | |
return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY ?? self.centerY), size: size) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment