问题描述
我有一个大的UIView
和许多小的subviews
.我需要找到一个区域内的所有subviews
.我目前正在遍历subviews
并使用CGRectContainsPoint
.可以,但是通常有90%的子视图不在我感兴趣的矩形内.
I have a large UIView
with many small subviews
. I need to find all subviews
within an area. I am currently iterating through subviews
and using CGRectContainsPoint
. This works, but 90% of the subviews are usually not within my rectangle of interest.
有没有更有效的方法来找到矩形内的所有subviews
?
Is there a more efficient way to find all subviews
within a rectangle?
谢谢
推荐答案
CGRectContainsRect
会更合适.您仍然需要根据对位置的假定情况来遍历矩形中的所有子视图,但是CGRectContainsRect
仍然比CGRectContainsPoint
更有意义.
CGRectContainsRect
would be more appropriate. You'd still need to loop through all subviews that might be in your rect based on what you can assume about their positions, but CGRectContainsRect
still makes more sense than CGRectContainsPoint
.
CGRect area = CGRectMake(10,10,200,200);
NSMutableArray *viewsWithinArea = [[NSMutableArray alloc] init];
for (UIView *aView in [self.view subviews]) {
if(CGRectContainsRect(area,aView.frame)) [views addObject:aView];
}
这篇关于在矩形内查找子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!