I have a point CGPoint in coordinates of the application window.
Is there a way to get a pointer to the view visible at that point?E.g. I have 100 views or random sizes and placed at random locations - some partially/fully covering each other.
Pressing a button in the app will list the address of the view visible at (0,0), (100,100) and at (200,200). The views may or may not have userInteraction enabled. Also, what about the more complicated scen开发者_如何学JAVAario where a view is covered by a fully transparent view?Updated
hitTest
will not work as it will not give you subtrees where a parent has userInteractionEnabled
set to NO
or a view which is outside of its parent's bounds.
visibleViewAtPoint
as described below will parse the entire view tree and give the view with the highest subview index. This should give the correct view (does not take z-order into consideration...)
- (void) findView:(UIView**)visibleView atPoint:(CGPoint)pt fromParent:(UIView*)parentView
{
UIView *applicationWindowView = [[[[UIApplication sharedApplication] keyWindow] rootViewController] view];
if(parentView == nil) {
parentView = applicationWindowView;
}
for(UIView *view in parentView.subviews)
{
if((view.superview != nil) && (view.hidden == NO) && (view.alpha > 0))
{
CGPoint pointInView = [applicationWindowView convertPoint:pt toView:view];
if([view pointInside:pointInView withEvent:nil]) {
*visibleView = view;
}
[self findView:visibleView atPoint:pt fromParent:view];
}
}
}
- (UIView*) visibleViewAtPoint:(CGPoint)pt
{
UIView *visibleView = nil;
[self findView:&visibleView atPoint:pt fromParent:nil];
return visibleView;
}
精彩评论