Masonry中查找View的公共公共父視圖Superview

View+MASAdditions.m

查找兩個(gè)view的公共父視圖Superview;

第一個(gè)view是自身self,第二個(gè)view是傳進(jìn)來(lái)的view;

第一層遍歷,找到第二個(gè)view的superView;

第二層遍歷,找到第一個(gè)view的superView,并判斷和第二個(gè)view的superView是否相等,

若相等賦值給closestCommonSuperview;

如果closestCommonSuperview有值了,就跳出循環(huán)了;先跳出里面的while,在外面的while;

- (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view {

? ? MAS_VIEW *closestCommonSuperview = nil;

? ? MAS_VIEW *secondViewSuperview = view;

? ? while (!closestCommonSuperview && secondViewSuperview) {

? ? ? ? MAS_VIEW *firstViewSuperview = self;

? ? ? ? while (!closestCommonSuperview && firstViewSuperview) {

? ? ? ? ? ? if (secondViewSuperview == firstViewSuperview) {

? ? ? ? ? ? ? ? closestCommonSuperview = secondViewSuperview;

? ? ? ? ? ? }

? ? ? ? ? ? firstViewSuperview = firstViewSuperview.superview;

? ? ? ? }

? ? ? ? secondViewSuperview = secondViewSuperview.superview;

? ? }

? ? return closestCommonSuperview;

}

NSArray+MASAdditions.m

查找數(shù)組中的子view的公共superView;

首先遍歷這個(gè)數(shù)組,只有UIView的class才繼續(xù)遍歷;

前一個(gè)view(previousView)不為空,查找和previousView的公共父視圖;

循環(huán)依次進(jìn)行下去;

- (MAS_VIEW *)mas_commonSuperviewOfViews

{

? ? MAS_VIEW *commonSuperview = nil;

? ? MAS_VIEW *previousView = nil;

? ? for (id object in self) {

? ? ? ? if ([object isKindOfClass:[MAS_VIEW class]]) {

? ? ? ? ? ? MAS_VIEW *view = (MAS_VIEW *)object;

? ? ? ? ? ? if (previousView) {

? ? ? ? ? ? ? ? commonSuperview = [view mas_closestCommonSuperview:commonSuperview];

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? commonSuperview = view;

? ? ? ? ? ? }

? ? ? ? ? ? previousView = view;

? ? ? ? }

? ? }

? ? NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy.");

? ? return commonSuperview;

}

---------------------

轉(zhuǎn)載自:https://blog.csdn.net/weixin_33843409/article/details/87472709

?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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