突然来的想法, 原来也很简单, 刚开始写了一个UIView的分类, 一个继承NSObject的类, 完全模仿Masonry的风格啊, 睡觉前觉得不好, 脑子里大概想了一下, 应该也可以优化成一个分类就够了, 早上试了一下, 果然只需要一个分类就够了呢
用起来就是这样的:
self.testView = [UIView new];
[self.view addSubview:self.testView];
self.testView.x(10.0f).y(0.0).w(100).h(100);
直接上代码了
@interface UIView (LNFrame)
typedef CGRect (^LNSetNewFrameBlock)(CGRect);
- (void)setNewFrame:(LNSetNewFrameBlock)newFrame;
- (UIView *(^)(CGFloat))x;
- (UIView *(^)(CGFloat))y;
- (UIView *(^)(CGFloat))w;
- (UIView *(^)(CGFloat))h;
@end
@implementation UIView (LNFrame)
- (void)setNewFrame:(CGRect (^)(CGRect frame))newFrame
{
if (newFrame) {
self.frame = newFrame (self.frame);
}
}
- (UIView *(^)(CGFloat))x
{
return ^UIView *(CGFloat x) {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
return self;
};
}
- (UIView *(^)(CGFloat))y
{
return ^UIView *(CGFloat y) {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
return self;
};
}
- (UIView *(^)(CGFloat))w
{
return ^UIView *(CGFloat w) {
CGRect frame = self.frame;
frame.size.width = w;
self.frame = frame;
return self;
};
}
- (UIView *(^)(CGFloat))h
{
return ^UIView *(CGFloat h) {
CGRect frame = self.frame;
frame.size.height = h;
self.frame = frame;
return self;
};
}
@end
网友评论