#import <UIKit/UIKit.h>
@interface UIView (PJXExtension)
@property(assign,nonatomic) CGFloat x; //x坐标
@property(assign,nonatomic) CGFloat y; //y坐标
@property(assign,nonatomic) CGFloat width; //宽度
@property(assign,nonatomic) CGFloat height; //高度
@property(assign,nonatomic) CGFloat centerX; //中心点X值
@property(assign,nonatomic) CGFloat centerY; //中心点Y值
@end
#import "UIView+PJXExtension.h"
@implementation UIView (PJXExtension)
//x
-(void)setX:(CGFloat)x{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(CGFloat)x{
return self.frame.origin.x;
}
//y
-(void)setY:(CGFloat)y{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
-(CGFloat)y{
return self.frame.origin.y;
}
//width
-(void)setWidth:(CGFloat)width{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(CGFloat)width{
return self.frame.size.width;
}
//height
-(void)setHeight:(CGFloat)height{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
-(CGFloat)height{
return self.frame.size.height;
}
//centerX
-(void)setCenterX:(CGFloat)centerX{
CGRect frame = self.frame;
frame.origin.x = centerX - frame.size.width/2.0;
self.frame = frame;
}
-(CGFloat)centerX{
return CGRectGetMidX(self.frame);
}
//centerY
-(void)setCenterY:(CGFloat)centerY{
CGRect frame = self.frame;
frame.origin.y = centerY - frame.size.height/2.0;
self.frame = frame;
}
-(CGFloat)centerY{
return CGRectGetMidY(self.frame);
}
/*
//中心X
-(CGFloat)centerX
{
return self.center.x;
}
-(void)setCenterX:(CGFloat)centerX
{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
//中心点Y
-(CGFloat)centerY
{
return self.center.y;
}
-(void)setCenterY:(CGFloat)centerY
{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
*/
@end
原文:http://www.cnblogs.com/PJXWang/p/5854811.html