gpt4 book ai didi

ios - 两个 View 之间的共享祖先

转载 作者:可可西里 更新时间:2023-11-01 04:10:53 24 4
gpt4 key购买 nike

找到两个 UIView 实例之间的最低公共(public)祖先的最有效方法是什么?

未实现Lowest Common Ancestor , 有没有可以利用的 UIKit API 来找到它?

NSViewancestorSharedWithView: 所以我怀疑这可能会被添加到 iOS 中。

我目前正在使用这种快速而肮脏的解决方案,如果给定的 View 不是兄弟 View 或直接祖先,则效率很低。

- (UIView*)lyt_ancestorSharedWithView:(UIView*)aView
{
if (aView == nil) return nil;
if (self == aView) return self;
if (self == aView.superview) return self;
UIView *ancestor = [self.superview lyt_ancestorSharedWithView:aView];
if (ancestor) return ancestor;
return [self lyt_ancestorSharedWithView:aView.superview];
}

(对于那些实现类似方法的人,Lyt 项目的单元测试可能会有所帮助)

最佳答案

这并不难,使用 -isDescendantOfView:。

- (UIView *)my_ancestorSharedWithView:(UIView *)aView
{
UIView *testView = self;
while (testView && ![aView isDescendantOfView:testView])
{
testView = [testView superview];
}
return testView;
}

关于ios - 两个 View 之间的共享祖先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22666535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com