masonry學(xué)習(xí)筆記

一标捺、封裝結(jié)構(gòu)

MASConstraint 約束
MASCompositeConstraint 一組約束//
MASViewConstraint 一個(gè)約束//囊括整個(gè)約束計(jì)算公式的所有數(shù)據(jù)(firstViewAttribute、secondViewAttribute揉抵、常量值亡容、關(guān)系、優(yōu)先級(jí)冤今、比例值闺兢。用于鏈?zhǔn)讲僮鞯摹癲elegate”)。

MASConstraintMaker 創(chuàng)建約束的工廠
MASViewAttribute 封裝 View+約束, 等式兩邊的計(jì)算因子(結(jié)構(gòu)相當(dāng)于元組類型)

View+MASAdditions 入口( 創(chuàng)建計(jì)算MASViewAttribute的工廠 )
ViewController+MASAdditions 入口( 創(chuàng)建MASViewAttribute的工廠 )

NSArray+MASAdditions 對(duì)數(shù)組中的每一個(gè)view做相同的約束操作屋谭。 批量操作脚囊。還提供了一套統(tǒng)一布局的方法。(固定大小桐磁,固定間距)
MASUtilities 用來(lái)將任何類型(數(shù)值類型悔耘,對(duì)象類型), 轉(zhuǎn)化為對(duì)象類型

二我擂、鏈?zhǔn)?/p>

1衬以、

make.height.inset(10);//無(wú)效。因?yàn)閘ayoutAttribute是NSLayoutAttributeHeight
make.height.mas_equalTo(10);

2校摩、

make.height.mas_equalTo(10); // 如果是NSValue類型看峻。給firstView設(shè)置
make.height.mas_equalTo(self.view);// 如果是View類型, 創(chuàng)建secondViewAttribute衙吩。并且屬性NSLayoutAttribute與firstViewAttribute相同
make.height.mas_equalTo(self.view.mas_height); //如果是計(jì)算因子類型互妓。則直接賦值。

3分井,

鏈?zhǔn)饺绾螌?shí)現(xiàn)的车猬?

MASCompositeConstraint(一組約束) 代理霉猛。
MASConstraintMaker (創(chuàng)建約束的工廠) 代理尺锚。

//操作1
make.top.offset(0);
make.bottom.offset(0);
make.size.sizeOffset(CGSizeZero);

//鏈?zhǔn)?
make.top.left.right.offset(0);

//鏈?zhǔn)?
make.edges.top.left.offset(0);

//鏈?zhǔn)?
make.top.equalTo(@20).equalTo(@30);

// 不支持這種鏈?zhǔn)剑?后面的-10會(huì)覆蓋前面的10
make.top.left.equalTo(@10).bottom.right.equalTo(@(-10));

    MASLayoutConstraint *existingConstraint = nil;
    if (self.updateExisting) {
        existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint];
    }
    if (existingConstraint) {
        // just update the constant
        existingConstraint.constant = layoutConstraint.constant;
        self.layoutConstraint = existingConstraint;
    }////////

//不支持重復(fù)添加約束
//equalTo(v2.mas_left)第二個(gè)約束,重復(fù)添加惜浅。
//NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation");

make.top.left.equalTo(v2.mas_top).equalTo(v2.mas_left).equalTo(@0);

// 不支持這種鏈?zhǔn)?br> make.center.size.edges

masConstraint

#pragma mark - attribute chaining

- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation");

    return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
}


masCompositeConstraint

#pragma mark - attribute chaining

- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
    return self;
}

- (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    id<MASConstraintDelegate> strongDelegate = self.delegate;
    MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
    newConstraint.delegate = self;
    [self.childConstraints addObject:newConstraint];
    return newConstraint;
}


- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint {
    NSUInteger index = [self.childConstraints indexOfObject:constraint];
    NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
    [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint];
}

maker

- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute];
}

- (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs {
    /////
    NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count];
    
    for (MASViewAttribute *a in attributes) {
        [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]];
    }
    
    MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children];
    constraint.delegate = self;
    [self.constraints addObject:constraint];
    return constraint;

}


- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
    MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute];
    MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute];
    if ([constraint isKindOfClass:MASViewConstraint.class]) {
        //replace with composite constraint
        NSArray *children = @[constraint, newConstraint];
        MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children];
        compositeConstraint.delegate = self;
        [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint];
        return compositeConstraint;
    }
    if (!constraint) {
        newConstraint.delegate = self;
        [self.constraints addObject:newConstraint];
    }
    return newConstraint;
}

- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint {
    NSUInteger index = [self.constraints indexOfObject:constraint];
    NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
    [self.constraints replaceObjectAtIndex:index withObject:replacementConstraint];
}

4瘫辩、設(shè)置布局值

嘗試用不支持的值設(shè)置布局常量.attempting to set layout constant with unsupported value
NSNumber,CGPoint坛悉,CGSize伐厌,UIEdgeInsets

嘗試添加不支持的屬性attempting to add unsupported attribute
UIView、NSValue裸影、MASViewAttribute

offset只是改變約束常量值
equalTo會(huì)增加約束關(guān)系(secondeViewAttribute)挣轨,如果是NSValue類型(與offset效果相似),根據(jù)約束的布局屬性NSLayoutAttribute是否匹配轩猩,設(shè)置對(duì)應(yīng)的常量值卷扮。

//相同
make.left.equalTo(@20);
make.left.offset(20);
//相同
make.left.equalTo(v2.mas_right).offset(20);
make.left.equalTo(v2.mas_right).equalTo(@20);

make.top.inset(0).insets(UIEdgeInsetsZero).offset(0).sizeOffset(CGSizeZero).centerOffset(CGPointZero).valueOffset([NSValue valueWithCGPoint:CGPointZero]).valueOffset( [NSValue value:&CGPointZero withObjCType:@encode(typeof((CGPointZero)))]);

//NSLayoutAttributeTop,NSLayoutAttributeLeft , 會(huì)造成崩潰
Constraint improperly relates anchors of incompatible types

make.top.equalTo(v2.mas_left);//崩潰
make.top.equalTo(@[v2.mas_top,v2.mas_left]);//崩潰

如果是第一個(gè)View不是大小約束均践,且第二個(gè)Item是nil晤锹,則第二個(gè)Item自動(dòng)設(shè)置為第一個(gè)View的父View,屬性關(guān)系與第一個(gè)View的屬性相同彤委。

+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鞭铆,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子焦影,更是在濱河造成了極大的恐慌车遂,老刑警劉巖封断,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異舶担,居然都是意外死亡澄港,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)柄沮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)回梧,“玉大人,你說(shuō)我怎么就攤上這事祖搓∮猓” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵拯欧,是天一觀的道長(zhǎng)详囤。 經(jīng)常有香客問(wèn)我,道長(zhǎng)镐作,這世上最難降的妖魔是什么驮瞧? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮秀存,結(jié)果婚禮上性誉,老公的妹妹穿的比我還像新娘。我一直安慰自己杨蛋,他們只是感情好兜材,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著逞力,像睡著了一般曙寡。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上寇荧,一...
    開(kāi)封第一講書(shū)人閱讀 51,737評(píng)論 1 305
  • 那天举庶,我揣著相機(jī)與錄音,去河邊找鬼揩抡。 笑死户侥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捅膘。 我是一名探鬼主播添祸,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼寻仗!你這毒婦竟也來(lái)了刃泌?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耙替,沒(méi)想到半個(gè)月后亚侠,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡俗扇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年硝烂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铜幽。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡滞谢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出除抛,到底是詐尸還是另有隱情狮杨,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布到忽,位于F島的核電站橄教,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏喘漏。R本人自食惡果不足惜护蝶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望翩迈。 院中可真熱鬧持灰,春花似錦、人聲如沸帽馋。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)绽族。三九已至,卻和暖如春衩藤,著一層夾襖步出監(jiān)牢的瞬間吧慢,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工赏表, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留检诗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓瓢剿,卻偏偏與公主長(zhǎng)得像逢慌,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子间狂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容