美文网首页
iOS - 用Masonry的风格设置Frame?

iOS - 用Masonry的风格设置Frame?

作者: 一只二货 | 来源:发表于2016-11-03 08:59 被阅读66次
    突然来的想法, 原来也很简单, 刚开始写了一个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
    
    

    相关文章

      网友评论

          本文标题:iOS - 用Masonry的风格设置Frame?

          本文链接:https://www.haomeiwen.com/subject/yfubuttx.html