检测 UIView 是否在父级中的另一个 UIView 后面 [英] Detect if a UIView is behind another UIView in parent
问题描述
我有一个带有多个子视图的父 UIView
.
I have a parent UIView
with several subviews.
子视图是兄弟视图 - 子视图不是彼此的子视图.(每个在他们的超视图中处于相同的层级)
The subviews are siblings - the child-views are not sub views of each other. (Each at the same heirarchical level in their superview)
例如:
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
能够检测到它已移回视图层次结构中.这怎么办?
I want yetAnotherChildView
to be able to detect it was moved back in the view heirarchy. How can this be done?
编辑:
我知道 containerView
可以知道什么子视图在什么之上 - 但我不想(不能)containerView
通知它的子视图他们的订单已更改 - 想象一下在股票视图之上添加一个子视图 - 股票视图不知道其子视图并且他们希望收到此类通知.
I understand that the containerView
can know what subviews are above what - but I don't wan't (can't) the containerView
notify its subviews that their order changed - Imagine adding a subview ontop of a stock view - the stock view has no idea of its subviews and that they would like to receive such a notification.
子视图 CustomUIView
需要观察其 superview
The subview CustomUIView
would need to observe some change in its superview
例如(这可能是一个非常糟糕的解决方案)
for instance (this probably is a very bad solution)
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
}
推荐答案
试试这个,它会给你给定视图后面的所有视图
Try this it will give you all views behind given view
-(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;}
这篇关于检测 UIView 是否在父级中的另一个 UIView 后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!