Skip to content

Instantly share code, notes, and snippets.

@domhofmann
Created November 28, 2018 18:07
Show Gist options
  • Save domhofmann/650c00c74c404450037d26ec79bd054b to your computer and use it in GitHub Desktop.
Save domhofmann/650c00c74c404450037d26ec79bd054b to your computer and use it in GitHub Desktop.
CGRect Convenience
//
// CGRect+Convenience.swift
//
// Created by Dom Hofmann on 9/5/18.
// Copyright © 2018 Dom Hofmann. All rights reserved.
//
import Foundation
import UIKit
extension CGRect {
public var top: CGFloat {
get {
return origin.y
}
set(value) {
origin.y = value
}
}
public var bottom: CGFloat {
get {
return origin.y + size.height
}
set(value) {
origin.y = value - size.height
}
}
public var left: CGFloat {
get {
return origin.x
}
set(value) {
origin.x = value
}
}
public var right: CGFloat {
get {
return origin.x + size.width
}
set(value) {
origin.x = value - size.width
}
}
public mutating func stretchRight(_ newRight: CGFloat) {
let currentRight = right
let diff = newRight - currentRight
size.width += diff
}
public mutating func stretchLeft(_ newLeft: CGFloat) {
let currentLeft = left
let diff = newLeft - currentLeft
left = newLeft
size.width -= diff
}
public mutating func stretchBottom(_ newBottom: CGFloat) {
let currentBottom = bottom
let diff = newBottom - currentBottom
size.height += diff
}
public mutating func stretchTop(_ newTop: CGFloat) {
let currentTop = top
let diff = newTop - currentTop
top = newTop
size.height -= diff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment