您的位置:首页 > 产品设计 > UI/UE

iOS给UIView添加常用的Extension

2016-05-13 22:43 295 查看
这个看个人习惯,不一定每个人的都一样。最好自己写一个,以后就用自己的。

#import <UIKit/UIKit.h>

@interface UIView (SetSize)

@property (nonatomic) CGFloat frameX;// ①x
@property (nonatomic) CGFloat frameY;// ②y
@property (nonatomic) CGFloat frameWidth;// width
@property (nonatomic) CGFloat frameHeight;// height
@property (nonatomic) CGFloat frameX_Width;// x + width(没有set方法)
@property (nonatomic) CGFloat frameY_Height;// y + height(没有set方法)

@property (nonatomic) CGFloat distance_right;// ③相对于父视图,距离右侧的距离
@property (nonatomic) CGFloat distance_bottom;// ④相对于父视图,距离底部的距离

@property (nonatomic) CGFloat centerX;// 中心点x
@property (nonatomic) CGFloat centerY;// 中心点y

@property (nonatomic) CGPoint origin;// origin
@property (nonatomic) CGSize size;// size

/**
*  相对于①的set方法,同样是设置了x,但是这个方法的width也变了,而距离右侧的距离不变
*/
- (void)setFrameXWithRightUnchanged:(CGFloat)frameX;
/**
*  相对于②的set方法,同样是设置了y,但是这个方法的height也变了,而距离底部的距离不变
*/
- (void)setFrameYWithBottomUnchanged:(CGFloat)frameY;
/**
*  相对于③的set方法,同样是设置了距离右侧的距离,但是这个方法的width也变了,而x不变
*/
- (void)setDistance_rightWithFrameXUnchanged:(CGFloat)distance_right;
/**
*  相对于④的set方法,同样是设置了距离底部的距离,但是这个方法的height也变了,而y不变
*/
- (void)setDistance_bottomWithFrameYUnchanged:(CGFloat)distance_bottom;

@end


#import "UIView+SetSize.h"

@implementation UIView (SetSize)

- (void)setFrameX:(CGFloat)frameX {
CGRect frame = self.frame;
frame.origin.x = frameX;
self.frame = frame;
}
- (CGFloat)frameX {
return self.frame.origin.x;
}

- (void)setFrameY:(CGFloat)frameY {
CGRect frame = self.frame;
frame.origin.y = frameY;
self.frame = frame;
}
- (CGFloat)frameY {
return self.frame.origin.y;
}

- (void)setFrameWidth:(CGFloat)frameWidth {
CGRect frame = self.frame;
frame.size.width = frameWidth;
self.frame = frame;
}
- (CGFloat)frameWidth {
return self.frame.size.width;
}

- (void)setFrameHeight:(CGFloat)frameHeight {
CGRect frame = self.frame;
frame.size.height = frameHeight;
self.frame = frame;
}
- (CGFloat)frameHeight {
return self.frame.size.height;
}

- (void)setFrameX_Width:(CGFloat)frameX_Width {

}
- (CGFloat)frameX_Width {
return self.frame.origin.x + self.frame.size.width;
}

- (void)setFrameY_Height:(CGFloat)frameY_Height {

}
- (CGFloat)frameY_Height {
return self.frame.origin.y + self.frame.size.height;
}

- (void)setDistance_right:(CGFloat)distance_right {
CGRect frame = self.frame;
frame.origin.x = self.superview.frame.size.width - self.frame.size.width - distance_right;
self.frame = frame;
}
- (CGFloat)distance_right {
return self.superview.frame.size.width - self.frame.origin.x - self.frame.size.width;
}

- (void)setDistance_bottom:(CGFloat)distance_bottom {
CGRect frame = self.frame;
frame.origin.y = self.superview.frame.size.height - self.frame.size.height - distance_bottom;
self.frame = frame;
}
- (CGFloat)distance_bottom {
return self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height;
}

- (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)setOrigin:(CGPoint)origin {
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGPoint)origin {
return self.frame.origin;
}

- (void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)size {
return self.frame.size;
}

- (void)setFrameXWithRightUnchanged:(CGFloat)frameX {
CGRect frame = self.frame;
frame.size.width -= frameX - frame.origin.x;
frame.origin.x = frameX;
self.frame = frame;
}
- (void)setFrameYWithBottomUnchanged:(CGFloat)frameY {
CGRect frame = self.frame;
frame.size.height -= frameY - frame.origin.y;
frame.origin.y = frameY;
self.frame = frame;
}
- (void)setDistance_rightWithFrameXUnchanged:(CGFloat)distance_right {
CGRect frame = self.frame;
frame.size.width -= distance_right - self.distance_right;
self.frame = frame;
}
- (void)setDistance_bottomWithFrameYUnchanged:(CGFloat)distance_bottom {
CGRect frame = self.frame;
frame.size.height -= distance_bottom - self.distance_bottom;
self.frame = frame;
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: