2011-04-14 48 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 
}