如何最有效地查找两个 UIView
实例之间的最低公共祖先?
除了实现 最低公共祖先,是否有任何可以利用的 UIKit
API 来查找?
NSView
有 ancestorSharedWithView:
,所以我猜这可能很快就会被添加到 iOS。
我目前正在使用这个简单粗暴的解决方案,如果给定的视图不是同级或直接祖先,则效率很低。
- (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 项目的单元测试可能会有所帮助)
superview
链向上走,然后找到它们分叉的位置。我不知道除了superview
之外还有什么API可以帮助。 - rmaddy