Skip to content

Instantly share code, notes, and snippets.

@cotkjaer
Last active November 23, 2023 06:15
Show Gist options
  • Select an option

  • Save cotkjaer/9b5af14a473e7c20fec1b0e965fe302b to your computer and use it in GitHub Desktop.

Select an option

Save cotkjaer/9b5af14a473e7c20fec1b0e965fe302b to your computer and use it in GitHub Desktop.

Revisions

  1. cotkjaer revised this gist Mar 18, 2017. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions CGRect+Center.swift
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@

    public extension CGRect
    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)
    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
    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
    */
    public var centerX: CGFloat
    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
    */
    public var centerY: CGFloat
    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
    */
    public func with(center: CGPoint?) -> CGRect
    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
    */
    public func with(centerX: CGFloat?) -> CGRect
    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
    */
    public func with(centerY: CGFloat?) -> CGRect
    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
    */
    public func with(centerX: CGFloat?, centerY: CGFloat?) -> CGRect
    func with(centerX: CGFloat?, centerY: CGFloat?) -> CGRect
    {
    return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY ?? self.centerY), size: size)
    }
  2. cotkjaer created this gist Mar 18, 2017.
    78 changes: 78 additions & 0 deletions CGRect+Center.swift
    Original 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)
    }
    }