检测一个UIView是否在父亲的另一个UIView后面

我有一个家长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将需要观察其CustomUIView一些变化

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

 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 } 

你可以扫描containerView.subviews并find哪个位置。 如果你想在每一个addSubview上做 – 在你的containerView的子类中添加你自己的调用父类实现的addSubview,然后发出子视图数组发生变化的通知(或者只是在那里执行检查)。 在通知处理程序中,您可以通过子视图进行扫描,并根据需要执行任何操作。

view.subviews数组中检查它们的indices 。 低指数的将位居榜首。 所以0 indexview将是所有其他index的顶部。

试试这个会给你所有观点背后的观点

 -(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;}