Swift之UIView的扩展
这样写代码阅读性更好,本人也喜欢这样写代码
Snip20180319_4.png
OC版本https://www.jianshu.com/p/87fe8966f36e!
第一步:
Snip20180319_5.png
第二步:
Snip20180319_6.png
第三步
代码
import Foundation import UIKit extension UIView{ public var LYW_X:CGFloat{ get{ return self.frame.origin.x } set{ var r = self.frame r.origin.x = newValue self.frame = r } } public var LYW_Y:CGFloat{ get{ return self.frame.origin.y } set{ var r = self.frame r.origin.y = newValue self.frame = r } } public var LYW_W:CGFloat{ get{ return self.frame.size.width } set{ var r = self.frame r.size.width = newValue self.frame = r } } public var LYW_H:CGFloat{ get{ return self.frame.size.height } set{ var r = self.frame r.size.height = newValue self.frame = r } } public var LYW_rightX:CGFloat{ get{ return self.LYW_X + self.LYW_W } set{ var r = self.frame r.origin.x = newValue - frame.size.width self.frame = r } } public var LYw_bottomY:CGFloat{ get{ return self.LYW_Y + self.LYW_H } set{ var r = self.frame r.origin.y = newValue - frame.size.height self.frame = r } } public var LYW_CenterX:CGFloat{ get{ return self.center.x } set { self.center = CGPoint(x: newValue, y: self.center.y) } } public var LYW_CenterY :CGFloat{ get{ return self.center.y } set{ self.center = CGPoint(x:self.center.x,y:newValue) } } public var LYW_origin:CGPoint{ get{ return self.frame.origin } set{ self.LYW_X = newValue.x self.LYW_Y = newValue.y } } public var LYW_size:CGSize{ get{ return self.frame.size } set{ self.LYW_W = newValue.width self.LYW_H = newValue.height } } }
自己动手多敲吧 ,使用方法就是拿到对象直接给它设置位置尺寸