我们在开发的过程中 经常会计算我们控件的frame 值
一般情况下都是这样的
获得x: self.frame.origin.x
获得y:self.frame.origin.y
获得width :self.frame.size.width
获得height:self.frame.size.height
这样写起来非常的麻烦 我们可以新建一个分类
重写frame 的set get 方法
这样我们可以实现单独对变量进行修改值
之前因为frame 是个结构体 嵌套两个结构体 所以没办法对其值进行单独的修改
#import <UIKit/UIKit.h>
@interface UIView (UIViewFrame)
//直接获得x 的值
@property(nonatomic,assign)CGFloat x;
//直接获得y 的值
@property(nonatomic,assign)CGFloat y;
//直接获得 width
@property(nonatomic,assign)CGFloat width;
//直接获得 height
@property(nonatomic,assign)CGFloat height;
@end
#import "UIView+UIViewFrame.h"
@implementation UIView (UIViewFrame)
//直接获得x 的值
-(void)setX:(CGFloat)x
{
CGRect rect = self.frame;
rect.origin.x = x;
self.frame = rect;
}
-(CGFloat)x
{
return self.frame.origin.x;
}
//直接获得y 的值
-(void)setY:(CGFloat)y
{
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
-(CGFloat)y
{
return self.frame.origin.y;
}
//直接获得 width
-(void)setWidth:(CGFloat)width
{
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
}
-(CGFloat)width
{
return self.frame.size.width;
}
//直接获得 height
-(void)setHeight:(CGFloat)height
{
CGRect rect = self.frame;
rect.size.height = height;
self.frame = rect;
}
-(CGFloat)height
{
return self.frame.size.height;
}
@end