iOS 开发探索-链式编程实战
r7AkGyjh
8年前
<p>“天下武功,无快不破”,求上天眷顾,愿各位技术大神来打通我的任督二脉,从此启动开挂模式,在技术进阶之路上狂奔.</p> <p>最近项目开发中用到了Masonry框架</p> <p>Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局 ,简洁明了,并具有高可读性,而且同时支持 iOS 和 Max OS X。</p> <p>我们先来看一段官方的sample code来认识一下Masonry</p> <pre> <code class="language-objectivec">[viewmas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(superview).with.insets(padding); }]; </code></pre> <p>相信各位大神早就熟知Masonry 这个开源框架,它主要采用链式编程思想.</p> <p>链式编程思想:是将多个操作(多行代码)通过点号(.)链接在一起成为一句代码,使代码可读性好。a(1).b(2).c(3)</p> <p>链式编程特点:方法的返回值是block,block必须有返回值(本身对象),block参数(需要操作的值)</p> <p>让我们来思考以下代码示例:</p> <pre> <code class="language-objectivec">UIView *redView = [UIViewnew]; redView.X(100).Y(20).Width(50).Height(100); redView.backgroundColor = [UIColorredColor]; [self.viewaddSubview:redView]; UIView *blueView = [UIViewnew]; blueView.Center(CGPointMake(150, 300)).Size(CGSizeMake(100, 50)); blueView.backgroundColor = [UIColorblueColor]; [self.viewaddSubview:blueView]; </code></pre> <p>大家有没有对以上代码布局感到奇怪,和我们通常的 frame 布局有所出入,请继续向下浏览</p> <p><img src="https://simg.open-open.com/show/389e4876543f9efa3ff54c04a9e3b5d7.jpg"></p> <p>按照链式编程思想,本人对 UIVIew+Frame 这个布局分类做了简单扩展.</p> <p><a href="/misc/goto?guid=4959723857177201060" rel="nofollow,noindex">源码下载</a></p> <p>UIView+Frame.h 头文件</p> <pre> <code class="language-objectivec">#import @interface UIView (Frame) - (UIView* (^)(CGFloat))X; - (UIView* (^)(CGFloat))Y; - (UIView* (^)(CGFloat))Width; - (UIView* (^)(CGFloat))Height; - (UIView* (^)(CGFloat))CenterX; - (UIView* (^)(CGFloat))CenterY; - (UIView* (^)(CGSize))Size; @end </code></pre> <p>UIView+Frame.m</p> <pre> <code class="language-objectivec">#import "UIView+Frame.h" @implementationUIView (Frame) - (UIView* (^)(CGFloat))X { return ^UIView*(CGFloat x){ CGRectframe = self.frame; frame.origin.x = x; self.frame = frame; return self; }; } - (UIView* (^)(CGFloat))Y { return ^UIView*(CGFloat y){ CGRectframe = self.frame; frame.origin.y = y; self.frame = frame; return self; }; } - (UIView* (^)(CGFloat))Width { return ^UIView*(CGFloatwidth){ CGRectframe = self.frame; frame.size.width = width; self.frame = frame; return self; }; } - (UIView* (^)(CGFloat))Height { return ^UIView*(CGFloatheight){ CGRectframe = self.frame; frame.size.height = height; self.frame = frame; return self; }; } - (UIView* (^)(CGFloat))CenterX { return ^UIView*(CGFloatcenterX){ CGRectframe = self.frame; frame.origin.x = centerX; self.frame = frame; return self; }; } - (UIView* (^)(CGFloat))CenterY { return ^UIView*(CGFloatcenterY){ CGRectframe = self.frame; frame.origin.y = centerY; self.frame = frame; return self; }; } - (UIView* (^)(CGSize))Size { return ^UIView*(CGSizesize){ CGRectframe = self.frame; frame.size = size; self.frame = frame; return self; }; } - (void)setX:(CGFloat)x { CGRectframe = self.frame; frame.origin.x = x; self.frame = frame; } - (CGFloat)x { return self.frame.origin.x; } - (void)setY:(CGFloat)y { CGRectframe = self.frame; frame.origin.y = y; self.frame = frame; } - (CGFloat)y { return self.frame.origin.y; } - (void)setWidth:(CGFloat)width { CGRectframe = self.frame; frame.size.width = width; self.frame = frame; } - (CGFloat)width { return self.frame.size.width; } - (void)setHeight:(CGFloat)height { CGRectframe = self.frame; frame.size.height = height; self.frame = frame; } - (CGFloat)height { return self.frame.size.height; } - (CGSize)size{ return self.frame.size; } - (void)setSize:(CGSize)size{ CGRectframe = self.frame; frame.size = size; self.frame = frame; } - (CGFloat)centerX{ return self.frame.origin.x; } - (void)setCenterX:(CGFloat)centerX{ CGRectframe = self.frame; frame.origin.x = centerX; self.frame = frame; } - (CGFloat)centerY{ return self.frame.origin.y; } - (void)setCenterY:(CGFloat)centerY{ CGRectframe = self.frame; frame.origin.y = centerY; self.frame = frame; } @end </code></pre> <p>笔者做为一名屌丝程序员,每天都在技术路上前行,由于本人天生愚钝,学艺不精,在此献丑啦!</p> <p> </p>