程序员们都在因为计算每个控件的frame发愁, 因为控件frame是一个结构体类型, 不能直接进行修改, 需要..., 所以这里问我为大家写了一个类, 直接创建一个类, 进行拷贝就可以使用
- 这种方法的头文件最好写到pch 文件中, 因为pch 文件中, 因为这种计算控件的frame太常用了
- 这里对CGPoint几种不常用的点没有定义
- 创建一个UIVeiw+Extension的分类
- 在.h文件中需要定义属性
- 注意点: 在分类中定义的属性, 是不会生成带下划线的成员变量的
#import <UIKit/UIKit.h>
@interface UIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGSize size;
@end
- 在.m文件中重写属性的getter 和setter方法, 对属性的frame进行修改
#import "UIView+Extension.h"
@implementation UIView (Extension)
-(void)setCenterX:(CGFloat)centerX {
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
-(CGFloat)centerX {
return self.center.x;
}
-(void)setCenterY:(CGFloat)centerY {
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
-(CGFloat)centerY {
return self.center.y;
}
-(void)setX:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(void)setY:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
-(CGFloat)x {
return self.frame.origin.x;
}
-(CGFloat)y {
return self.frame.origin.y;
}
-(void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(void)setHeight:(CGFloat)height{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
-(CGFloat)width {
return self.frame.size.width;
}
-(CGFloat)height {
return self.frame.size.height;
}
-(void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(CGSize)size {
return self.frame.size;
}
@end
请各位小伙伴多提意见