博客> iOS链式语法功能实践
iOS链式语法功能实践
2018-09-20 11:53 评论:0 阅读:485 二日三点
ios 链式语法

这个功能是个轮播图 项目中经常用到 可以看到代码语句确实减少了,可以想象 复杂界面 代码可以省下多少 一个普通的APP项目 界面不少于 50-70个 抛开复用界面 实际数量稳定30+ 那么链式语法可为我们减少大概三层的代码数量,甚至可以更加优秀

 Enter your image description here:

本次,使用扩展的方式将一些基本的控件做了扩展目的为了检验链式语法的简练程度 首先看下本次的功能代码结构

 2F86388E-3DBA-4FD0-837B-AC345942B562.png

自己写了一个基本控件的framework,(主要是尝试自己制作下framework),还蛮好的。

其次C层代码对比

#import "ViewController.h"
#import <MKChained> //frameWork
#import "Masonry.h"

@interface ViewController ()

@property (strong , nonatomic) NSArray *imageArray;

@end

@implementation ViewController

- (NSArray *)imageArray {
    if (!_imageArray) {
        _imageArray = @[@"ico_guide1", @"ico_guide2", @"ico_guide3",@"ico_guide4"];
    }
    return _imageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    /*
       //OC 段落式的写法
    UIScrollView *scrollView = [UIScrollView new];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setPagingEnabled:YES];
    [scrollView setBounces:NO];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

    UIImageView *lastImageView;
    for (int i = 0; i &lt; self.imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"ico_guide%d", i + 1]]];
        [imageView setContentMode:UIViewContentModeScaleToFill];
        [scrollView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (lastImageView) {
                make.left.equalTo(lastImageView.mas_right);
            }
            else {
                make.left.mas_equalTo(0);
            }
            make.top.mas_equalTo(0);
            make.width.height.equalTo(self.view);
        }];

        lastImageView = imageView;
    }

    [lastImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(scrollView.mas_right);
    }];
     //共22条语句
    */

    //链式写法
    UIScrollView *scrollView = [UIScrollView new].chainedScrollIndicator(NO,NO).chainedScrollEnabled(YES,YES).chainedScrollBounces(NO,YES);
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

    UIImageView *lastImageView;
    for (int i = 0; i &lt; self.imageArray.count; i++) {
        UIImageView *imageView = [UIImageView new].chainedImageViewContentSet([UIImage imageNamed:[NSString stringWithFormat:@"ico_guide%d", i + 1]],UIViewContentModeScaleToFill);
        [scrollView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (lastImageView) {
                make.left.equalTo(lastImageView.mas_right);
            }
            else {
                make.left.mas_equalTo(0);
            }
            make.top.mas_equalTo(0);
            make.width.height.equalTo(self.view);
        }];
        lastImageView = imageView;
    }

    [lastImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(scrollView.mas_right);
    }];

    //共17条语句 比段落式少5条
}

下周会直接放上framework库的cocopod地址 , 并且有相应的使用说明

下一篇: iOS链式语法(2) 数据模型中的使用 地址:http://blog.cocoachina.com/article/70294

收藏
0
sina weixin mail 回到顶部