iOS 开发 封装(X-Y-W-H)工具类
在代码中常常定义一个或者多个控件的高宽和xy坐标,例如:当控制单个控件的x时,自动布局略显冗余,而单个frame需要繁琐的来回定义,在这里封装一个控制xy和高宽的工具类,能显著的提高开发效率.
#import <UIKit/UIKit.h>
@interface UIView (Amals_Ex)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@end
#import "UIView+Amals_Ex.h"
@implementation UIView (Amals_Ex)
- (void)setY:(CGFloat)y {
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
- (CGFloat)y {
return self.frame.origin.y;
}
- (void)setX:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)x {
return self.frame.origin.x;
}
- (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)width {
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height {
return self.frame.size.height;
}
@end
本文介绍了一个用于iOS开发的自定义工具类,通过该工具类可以方便地设置UIView子类控件的位置和大小,包括x坐标、y坐标、宽度和高度等属性,极大地简化了布局代码。
3908

被折叠的 条评论
为什么被折叠?



