From df38eb2ead833bec3335baeb7dad7761186ef40f Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Tue, 17 Jul 2012 16:42:50 -0700 Subject: [PATCH] Added -[TUIView ancestorSharedWithView:] --- lib/UIKit/TUIView.h | 6 ++++++ lib/UIKit/TUIView.m | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/UIKit/TUIView.h b/lib/UIKit/TUIView.h index 20a0635..9c05479 100644 --- a/lib/UIKit/TUIView.h +++ b/lib/UIKit/TUIView.h @@ -304,6 +304,12 @@ extern CGRect(^TUIViewCenteredLayout)(TUIView*); - (void)willMoveToWindow:(TUINSWindow *)newWindow; - (void)didMoveToWindow; +/** + * Returns the closest ancestor that is shared by the receiver and another view, + * or nil if there is no such view. + */ +- (TUIView *)ancestorSharedWithView:(TUIView *)view; + /** Note: returns YES ff view == reciever */ diff --git a/lib/UIKit/TUIView.m b/lib/UIKit/TUIView.m index d4e2697..881fa0e 100644 --- a/lib/UIKit/TUIView.m +++ b/lib/UIKit/TUIView.m @@ -723,7 +723,7 @@ else CGContextSetRGBFillColor(context, 1, 0, 0, 0.3); CGContextFillRect(context, } else {\ [self.subviews insertObject:view atIndex:index];\ }\ - [view removeFromSuperview]; /* will call willAdd:nil and didAdd (nil) */ \ + [view removeFromSuperview]; /* will call willAdd:nil and didAdd (nil) */ \ [view willMoveToSuperview:self]; \ view.nsView = _nsView; @@ -835,6 +835,20 @@ else CGContextSetRGBFillColor(context, 1, 0, 0, 0.3); CGContextFillRect(context, #define END_EACH_SUBVIEW } +- (TUIView *)ancestorSharedWithView:(TUIView *)view +{ + TUIView *parentView = self; + + do { + if ([view isDescendantOfView:parentView]) + return parentView; + + parentView = parentView.superview; + } while (parentView); + + return nil; +} + - (BOOL)isDescendantOfView:(TUIView *)view { TUIView *v = self;