UIView的分类--iOS
Posted PJXWang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIView的分类--iOS相关的知识,希望对你有一定的参考价值。
#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
以上是关于UIView的分类--iOS的主要内容,如果未能解决你的问题,请参考以下文章