2012-12-19 34 views
1

我在屏幕中心有一個圓圈,我在這個固定圓圈(一個UIImageView)周圍分配了一系列UIlabels。標籤的數量由NSMutableArray中元素的數量給出,標籤的位置取決於標籤的數量。我無法給標籤賦予固定的x和y座標,因爲標籤的數量會有所不同。在特定位置分配UITextViews

我嘗試使用此代碼:

- (void)loadContent{ 

NSString *filename6 = [[NSUserDefaults standardUserDefaults]objectForKey:@"Setting"]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *groupPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", filename6]]; 

NSString *fileContent = [[NSString alloc] initWithContentsOfFile:groupPath]; 

NSMutableArray* array = [[NSMutableArray alloc] initWithArray:[fileContent componentsSeparatedByString:@", "]]; 

int arrayCount = array.count; 
int yCoordinate = (2*M_PI)/arrayCount; 
int xCoordinate = ; 

for(int i = 0; i < [array count]; i++){ 

    CGRect textframe = CGRectMake(the xcoordinate, the ycoordinate, 328, 30); 
    NSString *nameOfGroup = [array objectAtIndex:i]; 

    UITextView* theGroupTextLabel; 
    theGroupTextLabel = [[UITextView alloc] initWithFrame: textframe]; 
    [theGroupTextLabel setText: nameOfGroup]; 
    [theGroupTextLabel setTextColor: [UIColor redColor]]; 
    [self.view addSubview:theGroupTextLabel]; 

    //theGroupTextLabel.enabled = NO; 
    theGroupTextLabel.backgroundColor = [UIColor clearColor]; 

    theGroupTextLabel.layer.borderWidth = 3.5f; 
    theGroupTextLabel.layer.borderColor = [[UIColor blackColor] CGColor]; 

    int z; 
    z = z + 1; 
    theGroupTextLabel.tag = z; 

} 
} 

但我堅持找到正確的公式爲:

int yCoordinate = (2*M_PI)/arrayCount; 
int xCoordinate = ; 

...座標。有任何想法嗎?這是我正在使用的正確方法嗎?

+0

這些標籤是如何定位的?在網格中?或者它們圍繞圖像周圍環繞(如時鐘表面上的數字)?我注意到你有一個M_PI,所以我猜它就像一個時鐘? – Kekoa

+0

在圖像中以圓圈形式顯示,如同時鐘 – Alessandro

回答

1

這是一個比代碼問題更重要的數學問題,但我會採取一個刺。

假設您想要第一個項目在頂部,然後將項目順時針放置在主圖像周圍,您將在通過for循環時更改一個變量:角度。

然後,通過將角度和半徑(可能是常數)轉換爲笛卡爾座標,並將結果添加到圖像中心的座標,計算每次迭代中的x和y座標。這需要在for循環中完成,而不是像開始時那樣在代碼中完成。

您將在循環的每次迭代中添加到角度的數量爲2*M_PI/arrayCount

這大約是我在想什麼:

- (void)loadContent{ 

NSString *filename6 = [[NSUserDefaults standardUserDefaults]objectForKey:@"Setting"]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *groupPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", filename6]]; 

NSString *fileContent = [[NSString alloc] initWithContentsOfFile:groupPath]; 

NSMutableArray* array = [[NSMutableArray alloc] initWithArray:[fileContent componentsSeparatedByString:@", "]]; 

int arrayCount = array.count; 

CGFloat radius = 300;// <--- INSERT RADIUS HERE 
CGFloat angle = 0;// <--- The starting angle 
CGPoint center = CGPointMake(300,300); // <--- INSERT CENTER OF ARRANGEMENT 

for(int i = 0; i < [array count]; i++){ 
    int yCoordinate = radius * cos(angle) + center.y; 
    int xCoordinate = radius * sin(angle) + center.x; 

    CGRect textframe = CGRectMake(the xcoordinate, the ycoordinate, 328, 30); 
    NSString *nameOfGroup = [array objectAtIndex:i]; 

    UITextView* theGroupTextLabel; 
    theGroupTextLabel = [[UITextView alloc] initWithFrame: textframe]; 
    [theGroupTextLabel setText: nameOfGroup]; 
    [theGroupTextLabel setTextColor: [UIColor redColor]]; 
    [self.view addSubview:theGroupTextLabel]; 

    //theGroupTextLabel.enabled = NO; 
    theGroupTextLabel.backgroundColor = [UIColor clearColor]; 

    theGroupTextLabel.layer.borderWidth = 3.5f; 
    theGroupTextLabel.layer.borderColor = [[UIColor blackColor] CGColor]; 

    int z; 
    z = z + 1; 
    theGroupTextLabel.tag = z; 

    // INCREMENT ANGLE 
    angle += 2 * M_PI/array.count; 
} 
} 
+0

這段代碼很棒,但請您簡單介紹一下排列的中心和「在此插入半徑」是什麼? – Alessandro

+0

中心點是您希望項目從中輻射的位置,通常它將是大圓圈圖像中心的座標。半徑是從圓心到其邊緣的距離,這可能是大圓圖像長度的一半。我沒有在代碼中看到對該圖像的引用,或者我只是使用了該圖像,因此您必須自己填寫這些數字。 – Kekoa

+0

好的非常感謝,現在工作正常 – Alessandro

2

當與圈打交道時,你的X和Y座標是(cos angle, sin angle)如此序弄清楚你的x和y座標你應該這樣做的以下

float angle = (2*M_PI)/arrayCount; 
int xCoordinate = (cos(angle * i) * circleRadius) + circleCenterX; 
int yCoordinate = (sin(angle * i) * circleRadius) + circleCenterY; 

這會給你標籤應該出現在圓上的點。

N.B.您需要將angle乘以i以將下一個標籤移到正確的位置。在計算角度時,您可能還必須將1加到數組數,以防止最後一個與第一個標籤重疊。

來源:Wikipedia