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