2012-06-22 31 views
0

獲得CGFloat的最低值,我根據我得到的循環變量事件作爲下面的參數繪製reactangles在一個循環:找到一個循環

CGRectMake(cellWidth * event.xOffset,(cellHeight/MINUTES_IN_TWO_HOURS * [event minutesSinceEvent]), cellWidth,cellHeight/MINUTES_IN_TWO_HOURS * [event durationInMinutes]); 

在每一個循環的minutesSinceEventdurationInMinutes變化,所以每次都會繪製一個不同的矩形。

我想要得到循環中最低的y值和循環中最大的高度。簡單地說,我想要首先獲得矩形的y值。以及所有延伸的矩形的高度。

如果需要其他信息,請讓我知道?

回答

1

一個非常簡單的方法是將積累的所有矩形的聯合矩形:

CGRect unionRect = CGRectNull; 
for (...) { 
    CGRect currentRect = ...; 
    unionRect = CGRectUnion(unionRect, currentRect); 
} 
NSLog(@"min Y : %f", CGRectGetMinY(unionRect)); 
NSLog(@"height: %f", CGRectGetHeight(unionRect)); 

這樣做基本上是計算一個足夠大的矩形,以包含在循環中創建的所有矩形(但不會更大)。

+0

精彩:) thanks omz :) – keepsmiling

0

你可以做的是內循環之前聲明另一個CGRect變量,跟蹤值:

CGRect maxRect = CGRectZero; 
maxRect.origin.y = HUGE_VALF; //this is to set a very big number of y so the first one you compare to will be always lower - you can set a different number of course... 
for(......) 
{ 
    CGRect currentRect = CGRectMake(cellWidth * event.xOffset,(cellHeight/MINUTES_IN_TWO_HOURS * [event minutesSinceEvent]), cellWidth,cellHeight/MINUTES_IN_TWO_HOURS * [event durationInMinutes]); 

    if(currentRect.origin.y < maxRect.origin.y) 
     maxRect.origin.y = currentRect.origin.y; 

    if(currentRect.size.height > maxRect.size.height) 
     maxRect.size.height = currentRect.size.height; 
} 

//After the loop your maxRect.origin.y will be the lowest and your maxRect.size.height will be the greatest...