2011-04-14 51 views
0

我使用的是NSArray包含CGRect值。有没有办法打通库方法包含在阵列中的所有rects的最大宽度,或者说我要实现我自己的逻辑?提前致谢。寻找rects的最大宽度的NSArray

代码示例:

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。我从XML获得帧array1。我解析了这些帧并将它们放入数组中。我以非常简单的方式提供了我的代码。

+0

发布你的代码可能是有用的 – visakh7 2011-04-14 10:34:38

+0

正确你有不同的帧值的数组。我想你需要的是对它们进行排序像您挑选任何数组,但排序具有基于宽度来完成。 – visakh7 2011-04-14 10:35:12

+0

是7KV7为寻找对我的问题究竟感谢.. – ajay 2011-04-14 10:39:55

回答

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. 
+0

感谢响应我会用上面的代码尝试.. – ajay 2011-04-14 11:12:46

+0

谢谢你的工作.... – ajay 2011-04-14 11:28:21

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 
}