查找2个视图层的目标共同父视图
程序员文章站
2022-07-03 16:51:51
思路:记录两个视图层的所有父视图并分别保存到各自的父视图数组容器中,倒序寻找,直至匹配结果首次出现不一样的视图记录下索引序号index,则(index-1)序号坑位中即为目标共同父视图。//算法:共同父视图元素组- (NSArray *)findCommonSuperViews:(UIView *)viewO viewT:(UIView *)viewT { NSArray *superViews1 = [self findSuperViews:viewO]; NSArray *sup....
思路:记录两个视图层的所有父视图并分别保存到各自的父视图数组容器中,倒序寻找,直至匹配结果首次出现不一样的视图记录下索引序号index,则(index-1)序号坑位中即为目标共同父视图。
//算法:共同父视图元素组
- (NSArray *)findCommonSuperViews:(UIView *)viewO viewT:(UIView *)viewT {
NSArray *superViews1 = [self findSuperViews:viewO];
NSArray *superViews2 = [self findSuperViews:viewT];
NSMutableArray *resultArray = [NSMutableArray array];
int i = 0;
unsigned long CNT = MIN(superViews1.count,superViews2.count);
//***************关键核心*******************
while (i < CNT) {//倒序方式读取元素
UIView *super1 = superViews1[superViews1.count - i - 1];
UIView *super2 = superViews2[superViews2.count - i - 1];
if (super1 == super2) {
[resultArray addObject:super1];
i++;
}else{
break;
}
}
//**********************************
NSArray *comSuperViews = [NSArray arrayWithArray:resultArray];
return comSuperViews;
}
//记录视图层的所有父视图并保存到父图元素数组容器中
- (NSArray<UIView *>*)findSuperViews:(UIView *)view {
UIView *temp = view.superview;
NSMutableArray *result = [NSMutableArray array];
while (temp) {
[result addObject:temp];
temp = temp.superview;
}
return result;
}
本文地址:https://blog.csdn.net/yanhaijunyan/article/details/109386422
下一篇: 黄月英真的很聪明吗?她有哪些主要的事迹?