在矩形内查找子视图
我有一个很大的UIView
与许多小的subviews
。 我需要find一个区域内的所有subviews
。 我目前通过subviews
迭代和使用CGRectContainsPoint
。 这个工作,但90%的子视图通常不在我感兴趣的矩形内。
有没有更有效的方法来查找矩形内的所有subviews
?
谢谢
CGRectContainsRect
会更合适。 你仍然需要循环遍历所有可能位于你的矩形中的子视图,但是CGRectContainsRect
比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]; }
@ james_womack在Swift中的回答:
func subviewsWithin(area: CGRect) -> [UIView] { return subviews.filter { area.contains($0.frame) } }