Skip to content

Instantly share code, notes, and snippets.

@shar0
Last active July 2, 2018 09:58
Show Gist options
  • Save shar0/ff9274b5a506b2d8bc51ef4ddd49cbaa to your computer and use it in GitHub Desktop.
Save shar0/ff9274b5a506b2d8bc51ef4ddd49cbaa to your computer and use it in GitHub Desktop.
I'm tired to write more frame.origin.x/y or frame.size.width/height.
//
// ViewExtension.swift
// Rina
//
// Created by Joe Wang on 02/07/2018.
// License: WTFPL
//
import UIKit
extension UIView {
var x:CGFloat {
get {
return frame.origin.x
}
set {
frame = CGRect(x: newValue, y: y, width: width, height: height)
}
}
var y:CGFloat {
get {
return frame.origin.y
}
set {
frame = CGRect(x: x, y: newValue, width: width, height: height)
}
}
var width:CGFloat {
get {
return frame.size.width
}
set {
frame = CGRect(x: x, y: y, width: newValue, height: height)
}
}
var height:CGFloat {
get {
return frame.size.height
}
set {
frame = CGRect(x: x, y: y, width: width, height: newValue)
}
}
var origin:CGPoint {
get {
return frame.origin
}
set {
frame = CGRect(x: newValue.x, y: newValue.y, width: width, height: height)
}
}
var size:CGSize {
get {
return frame.size
}
set {
frame = CGRect(x: x, y: y, width: newValue.width, height: newValue.height)
}
}
func getRelativeX(_ offset:CGFloat = 0 ) -> CGFloat {
return x + width + offset
}
func getRelativeY(_ offset:CGFloat = 0 ) -> CGFloat {
return y + height + offset
}
func getCenterX(_ width:CGFloat, _ offset:CGFloat = 0 ) -> CGFloat {
return (width - self.width) / 2 + offset
}
func getCenterY(_ height:CGFloat, _ offset:CGFloat = 0 ) -> CGFloat {
return (height - self.height) / 2 + offset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment