0

我正在使用NSArray包含CGRect值的。有没有办法通过库方法获得数组中包含的所有矩形的最大宽度,还是我必须实现自己的逻辑?提前致谢。

代码示例:

for (int i=0; i<[array1 count]; i++) {
    CGRect rect = [[array1 objectAtIndex:i] CGRectValue];
// Here, some transformation of rects

[array2 addObject:[NSValue valueWithCGRect:rect]];

}

在这段代码中,我试图从中获取帧array1并将它们添加到array2. 我array1从 XML 中获取框架。我解析了这些帧并将它们放入数组中。我以一种非常简单的方式提供了我的代码。

4

2 回答 2

1

不要-count像那样一遍又一遍地向您的阵列发送消息。这很浪费。

float result = 0.0;
for (value in array1)
  {
  GCRect rect = [value CGRectValue];
  result = MAX(result, rect.size.width);
  }  // "result" now contains the greatest width you saw in the loop.
于 2011-04-14T11:04:53.823 回答
0
for(int i=0; i < [array2 count]; i++) 
{
    CGRect rect = [[array2 objectAtIndex:i] CGRectValue];
    float widthTest = rect.size.width;
    //this gives you the width of each element. Compare and get the biggest
}
于 2011-04-14T11:11:44.680 回答