swift关于UIView设置frame值的extension
使用
说明
1. 使用如上图,很简单,不再赘述
2. 在extension给添加的计算属性提供getter,setter方法即可
源码
//
// UIView+SetRect.swift
// YouXianMing
//
// Created by YouXianMing on 15/9/29.
// Copyright 2015年 YouXianMing All rights reserved.
//
import UIKit
extension UIView {
// x
var x : CGFloat {
get {
return frame.origin.x
}
set(newVal) {
var tmpFrame : CGRect = frame
tmpFrame.origin.x = newVal
frame = tmpFrame
}
}
// y
var y : CGFloat {
get {
return frame.origin.y
}
set(newVal) {
var tmpFrame : CGRect = frame
tmpFrame.origin.y = newVal
frame = tmpFrame
}
}
// height
var height : CGFloat {
get {
return frame.size.height
}
set(newVal) {
var tmpFrame : CGRect = frame
tmpFrame.size.height = newVal
frame = tmpFrame
}
}
// width
var width : CGFloat {
get {
return frame.size.width
}
set(newVal) {
var tmpFrame : CGRect = frame
tmpFrame.size.width = newVal
frame = tmpFrame
}
}
// left
var left : CGFloat {
get {
return x
}
set(newVal) {
x = newVal
}
}
// right
var right : CGFloat {
get {
return x + width
}
set(newVal) {
x = newVal - width
}
}
// top
var top : CGFloat {
get {
return y
}
set(newVal) {
y = newVal
}
}
// bottom
var bottom : CGFloat {
get {
return y + height
}
set(newVal) {
y = newVal - height
}
}
var centerX : CGFloat {
get {
return center.x
}
set(newVal) {
center = CGPoint(x: newVal, y: center.y)
}
}
var centerY : CGFloat {
get {
return center.y
}
set(newVal) {
center = CGPoint(x: center.x, y: newVal)
}
}
var middleX : CGFloat {
get {
return width / 2
}
}
var middleY : CGFloat {
get {
return height / 2
}
}
var middlePoint : CGPoint {
get {
return CGPoint(x: middleX, y: middleY)
}
}
}
时间: 2024-12-30 02:27:14