今天突然有人问我,怎么给UIView添加分类,使得在外面调用点语法获得结构体的值更为简单快捷。
这个问题对于知道的人来说是个再简单不过的问题了,但是对于不知道的人来说确实如同蜀道一样,有这个
疑问的朋友注意了,是时候该看看基础知识@property.
我在此附上源代码,会的朋友当成是温习,不会的朋友可以借鉴一下。
#import <UIKit/UIKit.h>
@interface UIView (ZQ_ViewFrame)
@property CGFloat width;
@property CGFloat height;
@property CGFloat x;
@property CGFloat y;
@property CGFloat centerX;
@property CGFloat centerY;
@end
#import "UIView+ZQ_ViewFrame.h"
@implementation UIView (ZQ_ViewFrame)
-(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;
}
-(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;
}
- (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;
}
@end