我在特定位置有一个图像视图,我知道该位置,但我想检查该位置是否有一个图像视图。
请帮我解决
Here is a helper method to check if two rects overlap:
-(BOOL) rect:(CGRect)rect overlap:(CGRect)anotherRect
{
BOOL xOverlap = ((rect.origin.x >= anotherRect.origin.x) && (rect.origin.x <= anotherRect.origin.x + anotherRect.size.width)) ||
((anotherRect.origin.x >= rect.origin.x) && (anotherRect.origin.x <= rect.origin.x + rect.size.width));
BOOL yOverlap = ((rect.origin.y >= anotherRect.origin.y) && (rect.origin.y <= anotherRect.origin.y + anotherRect.size.height)) ||
((anotherRect.origin.y >= rect.origin.y) && (anotherRect.origin.y <= rect.origin.y + rect.size.height));
return xOverlap && yOverlap;
}
Usage:
You can iterate though all subviews of a view and check if the frame of your view overlaps another view's frame. Suppose you have a property myView then:
for(UIView *v in self.view.subviews)
{
if(v == self.myView) //skip the same view
continue;
if([self rect:self.myView.frame overlap:v.frame])
{
//Do something...
}
}
首先,您在主视图中获得所有视图的数组:
NSArray *subviews = [view subviews];
然后,对于每个视图,您可以检查每个视图的框架:
for (int i = 0 ; i < [subviews count]; i++){
CGRect frame = [subviews objectAtIndex:i].frame;
}
您可以在 for 循环中浏览该 UIImageView 的父视图的子视图。并将每个视图的位置与特定 UIImageView 的位置进行比较。
但这对我来说听起来很糟糕。也许如果你能告诉更多关于这个问题的信息,我们可以提供更好的帮助。
您也可以使用这种方法:
既然你说你有 ImageView 的位置,那么将它保存在一些 CGRect 变量中。试试这个
UIImageView *yourImgView = (UIImageView *)[view subviews];
这将返回您的 imageView 。然后,您可以根据需要检查其较早保存的位置或其可见性的框架。