2

我有一个UIView有几个子视图的父母。

子视图是兄弟 - 子视图不是彼此的子视图。(每个人都在他们的超级视图中处于相同的层次结构)

例如:

UIView *containerView = [[UIView alloc] initWithFrame:frame];
CustomUIView *childView = [[UIView alloc] initWithFrame:anotherFrame];
CustomUIView *yetAnotherChildView = [[UIView alloc] initWithFrame:anotherFrame];

[containerView addSubview:childView];
[containerView addSubview:yetAnotherChildView];

[containerView bringSubviewToFront:childView];

我希望yetAnotherChildView能够检测到它在视图层次结构中被移回。如何才能做到这一点?

编辑

我知道containerView可以知道哪些子视图高于什么-但我不想(不能)containerView通知其子视图他们的订单已更改-想象在股票视图上添加子视图-股票视图不知道的子视图,并且他们希望收到这样的通知。

子视图CustomUIView需要观察它的一些变化superview

例如(这可能是一个非常糟糕的解决方案)

CustomUIView

-(id)initWithFrame
{
  [self.superView addObserver:self forKeyPath:@"subViews" options:options context:context];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
 // if keyPath is "subViews" array check if "this" view is ontop and adjust self accordingly
}
4

3 回答 3

1

你可以通过containerView.subviews 扫描,找到哪个在哪个位置。如果您想在每个 addSubview 上执行此操作 - 将您的 containerView 子类化并添加您自己的 addSubview 调用父类实现,然后发出子视图数组更改的通知(或者只是在那里进行检查)。在通知处理程序中,您可以扫描子视图并按顺序做任何您想做的事情。

于 2014-01-29T13:09:51.033 回答
1

检查他们indicesview.subviews数组。指数低的将在顶部。所以viewat 0index将在所有其他的顶部。

于 2014-01-29T13:08:08.813 回答
1

试试这个,它会给你给定视图后面的所有视图

-(NSMutableArray *)getAllViewsBehindView:(UIView *)view{
NSMutableArray *tempArr = [NSMutableArray array];

NSArray *subViews = view.superview.subviews;

for (int i = 0; (i < subViews.count); i++)
{
    UIView *viewT = [subViews objectAtIndex:i];

    if (viewT == view)
    {
        return tempArr;
    }
    else
    {
        if (CGRectIntersectsRect(viewT.frame, view.frame))
        {
            [tempArr addObject:viewT];
        }
    }

}

return tempArr;}
于 2014-01-29T13:49:18.877 回答