iOS 鏈?zhǔn)骄幊?初探

什么是鏈?zhǔn)骄幊?,大家第一印象可能是鏈條?,個(gè)人喜歡樂高Lego一環(huán)扣一環(huán),不需要按順序進(jìn)行連接,每一個(gè)環(huán)就是一個(gè)組件,隨意組合亦得,重復(fù)組合又得,得咗!<不懂粵語的朋友不用在意這一句>。
鏈?zhǔn)骄幊痰暮诵乃枷刖褪前讶哂嗟拇a封裝起來,通過語法糖syntactic sugar,one by one一個(gè)一個(gè)的連接起來,使代碼更加方便管理,可讀性更加好,而且使用起來更加方便。

鏈?zhǔn)骄幊痰拇?Masonry,SDAutoLayout,自動(dòng)布局的好幫手??,不過我沒有用過,真的好慚愧,想打自己的臉( ̄ε(# ̄)☆╰╮( ̄▽ ̄///)。

說了這么多廢話,來點(diǎn)正經(jīng)的,鏈?zhǔn)骄幊痰暮诵恼Z法:block
Tips:不懂block的朋友請(qǐng)移玉步,不需要你對(duì)block的熟練程度從入門到放棄,但至少熟練掌握block。不然這篇文章對(duì)你而言只是copy and paste

#import <UIKit/UIKit.h>

@interface ButtonChain : UIButton
//初始化方法
+(ButtonChain *(^)(void))initialization;

//自定義frame
-(ButtonChain *(^)(CGRect rect))rect;

//自定義backgroundColor
-(ButtonChain *(^)(UIColor * color))bgColor;

//自定義title
-(ButtonChain *(^)(NSString * title))normalTitle;

//自定義selectTitle
-(ButtonChain *(^)(NSString * title))selectTitle;

//自定義target action
-(ButtonChain *(^)(id object, SEL method))action;

@end

可以先無視+(ButtonChain *(^)(void))initialization;這個(gè)類方法,因?yàn)殒準(zhǔn)骄幊讨蛔饔迷趯?duì)象上,與類無關(guān)。
其實(shí)上面的對(duì)象方法就是get方法,所以我們才可以通過語法糖syntactic sugar點(diǎn)出來,當(dāng)我們轉(zhuǎn)換一下思維就ok了,看一下轉(zhuǎn)換方法你就懂了

#import "UIButton+Test.h"

typedef UIButton *(^Chain)(UIFont *font);

@interface UIButton (Test)

@property (nonatomic,copy,readonly) Chain font;

@end

@implementation UIButton (Test)

-(Chain)font {
    return ^id(UIFont * font) {
        self.titleLabel.font = font;
        return self;
    };
}

@end

下面是實(shí)現(xiàn)部分:

#import "ButtonChain.h"

@implementation ButtonChain

+(ButtonChain *(^)(void))initialization {
    return ^id(void) {
        return [ButtonChain buttonWithType:UIButtonTypeCustom];
    };
}

-(ButtonChain *(^)(CGRect))rect {
    return ^id(CGRect rect) {
        return [self addRect:rect];
    };
}

-(ButtonChain *(^)(UIColor *))bgColor {
    return ^id(UIColor * color) {
        return [self addBgColor:color];
    };
}

-(ButtonChain *(^)(NSString *))normalTitle {
    return ^id(NSString * title) {
        return [self addNormalTitle:title];
    };
}

-(ButtonChain *(^)(NSString *))selectTitle {
    return ^id(NSString * title) {
        return [self addSelectTitle:title];
    };
}

-(ButtonChain *(^)(id, SEL))action {
    return ^id(id object, SEL method) {
        return [self addTarget:object action:method];
    };
}

/*-------------------------分割線----------------------------*/
-(ButtonChain *)addRect:(CGRect)rect {
    self.frame = rect;
    return self;
}

-(ButtonChain *)addBgColor:(UIColor *)bgColor {
    self.backgroundColor = bgColor;
    return self;
}

-(ButtonChain *)addNormalTitle:(NSString *)title {
    [self setTitle:title forState:UIControlStateNormal];
    return self;
}

-(ButtonChain *)addSelectTitle:(NSString *)title {
    [self setTitle:title forState:UIControlStateSelected];
    return self;
}

-(ButtonChain *)addTarget:(id)object action:(SEL)action {
    [self addTarget:object action:action forControlEvents:UIControlEventTouchUpInside];
    return self;
}

@end

每個(gè)方法都需要你返回一個(gè)block,而且這個(gè)block需要你返回一個(gè)對(duì)象,你可以沒有參數(shù),但不能沒有對(duì)象,不然就會(huì)斷鏈,就好像我們這群沒有對(duì)象的碼農(nóng)一樣,從而變成一個(gè)single dog??,不好意思,用錯(cuò)了字眼,不是我們,是我 ??。美好的開始,孤獨(dú)的終結(jié)

#import "ViewController.h"
#import "ButtonChain.h"

@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    //初始化一個(gè)UIButton
    UIButton * btnChain = [ButtonChain initialization]().rect(CGRectMake((width-100)/2, height/2, 100, 40))
                                                        .normalTitle(@"normal")
                                                        .bgColor([UIColor blackColor])
                                                        .action(self, @selector(changeSelected:));

    [self.view addSubview:btnChain];
}

-(void)changeSelected:(ButtonChain *)sender {
    sender.selected = !sender.selected;
    if (!sender.selected) {
        sender.normalTitle(@"normal").bgColor([UIColor blackColor]);
    }
    else {
        sender.selectTitle(@"selected").bgColor([UIColor purpleColor]);
    }
}
@end

我們看看效果吧!

chainGIF.gif

上面初始化了一個(gè)UIButton,不過在寫法上不夠嚴(yán)謹(jǐn),應(yīng)該是這樣

ButtonChain * btnChain = [ButtonChain initialization]().rect(CGRectMake((width-100)/2, height/2, 100, 40))
                                                        .normalTitle(@"normal")
                                                        .bgColor([UIColor blackColor])
                                                        .action(self, @selector(changeSelected:));

因?yàn)檫@是自定義了一個(gè)ButtonChain,而不是對(duì)UIButton進(jìn)行擴(kuò)展的分類category,所以btnChain點(diǎn)不出任何東西。然后你會(huì)說:你這不是在坑我嗎,這么菜的代碼還寫上來,但是只要你理解內(nèi)存地址這東東,也是不妨礙我們閱讀的。從而可以看出鏈?zhǔn)骄幊痰膶?duì)我們的作用,需要什么就點(diǎn)什么,而不像我們寫分類方法時(shí),需要多個(gè)參數(shù),有時(shí)為了迎合需求,寫出N個(gè)分類方法;或者接手你項(xiàng)目的新手,有時(shí)候還會(huì)繼續(xù)擴(kuò)展各種方法,想一想都覺得茶煲trouble

+(UIButton *)createWith:(CGRect)frame title:(NSString *)title fontSize:(CGFloat)font ... {
    ...
}

這里只是簡單介紹了一下鏈?zhǔn)骄幊蹋垂僖部梢宰约簞?dòng)手寫寫,對(duì)其他UI控件進(jìn)行自定義封裝,滿足自己日常的使用,歡迎討論!
demo在此 給個(gè)star我也是很歡喜的??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容