.h 文件
#import <UIKit/UIKit.h>
@interface UIView (YYExtension)
// 在分类中 @property 只会声明方法, 不会生成方法的实现 和 _成员属性
/**X坐标*/
@property (nonatomic, assign) CGFloat x;
/**Y坐标*/
@property (nonatomic, assign) CGFloat y;
/**宽度*/
@property (nonatomic, assign) CGFloat width;
/**高度*/
@property (nonatomic, assign) CGFloat height;
/**大小*/
@property (nonatomic, assign) CGSize size;
/**中心点X*/
@property (nonatomic, assign) CGFloat centerX;
/**中心点Y*/
@property (nonatomic, assign) CGFloat centerY;
@end
.m 文件
#import "UIView+YYExtension.h"
@implementation UIView (YYExtension)
- (CGFloat)x{
return self.frame.origin.x;
}
- (void)setX:(CGFloat)x{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)y{
return self.frame.origin.y;
}
- (void)setY:(CGFloat)y{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)width{
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)height{
return self.frame.size.height;
}
- (void)setHeight:(CGFloat)height{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGSize)size{
return self.frame.size;
}
- (void)setSize:(CGSize)size{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGFloat)centerX{
return self.center.x;
}
- (void)setCenterX:(CGFloat)centerX{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
- (CGFloat)centerY{
return self.center.y;
}
- (void)setCenterY:(CGFloat)centerY{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
@end
注意:使用前 添加头文件#import "UIView+YYExtension.h"
网友评论