0

我有 4 个视图并用 4 个按钮控制这些视图,我有点困惑如何检查哪个视图位于顶部,我已经尝试过isFirstResponder但它不起作用。如果有人知道,请帮助我。提前致谢。

4

3 回答 3

7

要从子视图中获取最顶层的视图,请从父视图中获取 lastObject

UIView * topMost = [[self.view subviews] lastObject];
于 2012-06-28T13:22:37.120 回答
1

一开始,当我尝试设置将显示在同一区域中的视图时,曾经对我有帮助的一件事是,我曾经为每个视图设置不同的背景颜色,然后查看哪个在顶部。我不知道这是否是您尝试进行的设置,但您可以做的另一件事是刷新包含视图,这样您就只有“仅”想要的当前视图。

于 2012-06-28T13:30:03.787 回答
0

像这样

UIWindow *window = [[UIApplication sharedApplication].keyWindow;
[window findTopMostViewForPoint:CGPointMake(100, 100)];

功能

@implementation UIView (Extra)

- (UIView *)findTopMostViewForPoint:(CGPoint)point
{
    for(int i = self.subviews.count - 1; i >= 0; i--)
    {
        UIView *subview = [self.subviews objectAtIndex:i];
        if(!subview.hidden && CGRectContainsPoint(subview.frame, point))
        {
            CGPoint pointConverted = [self convertPoint:point toView:subview];
            return [subview findTopMostViewForPoint:pointConverted];
        }
    }

    return self;
}

- (BOOL)isTopmostViewInWindow
{
    if(self.window == nil)
    {
        return NO;
    }

    CGPoint centerPointInSelf = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGPoint centerPointOfSelfInWindow = [self convertPoint:centerPointInSelf toView:self.window];
    UIView *view = [self.window findTopMostViewForPoint:centerPointOfSelfInWindow];
    BOOL isTopMost = view == self || [view isDescendantOfView:self];
    return isTopMost;
}

@end
于 2013-02-06T18:09:59.923 回答