2013-08-16 40 views
0

1)我有一個長度爲10的數組,並命名爲'pointArrar'。該數組在每個索引上包含CGPoints
2)另外我有10個按鈕。
現在我想從CGPoint數組中設置Buttons框架(只有x & y位置)。
我只想知道如何設置數組中包含的CGPoint的按鈕x和y位置。從CGPoint數組設置按鈕框(僅x和y位置)

回答

1

那麼它非常合乎邏輯。

比方說你的陣列就像

NSArray * pointArrar = [NSArray arrayWithObjects: 
        [NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)], 
        [NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)], 
        nil]; 

然後添加到您需要簡單地把所有的點成一個圈狀

for(int i=0; i<[pointArrar count] ; i++) 
{ 
    NSValue *val = [pointArrar objectAtIndex:i]; 
    CGPoint p = [val CGPointValue]; 
    [Button setCenter:p]; 
} 

這裏已經添加了所有點的各種按鈕到你的按鈕。希望這適用於你:)

+1

不錯的工作!謝謝:))工作正常..我有一個問題,我是按鈕動畫,並把它們放在一個地方。我想要最近的按鈕上面的所有。我使用sendSubViewToBack:方法發送前一個按鈕回來,但這是行不通的...我想你的或其他的建議在這..再次感謝 –

+0

而不是你可以刪除以前的按鈕或隱藏它們或減少它們的阿爾法他們沒有看到... – IronManGill

+0

先生其實每一個按鈕都必須顯示,但最近的一個會在前一個.. –

0

據我所知,你想使用NSArray它不存儲任何結構。所以你可以讓你NSValue包裝你的觀點並將它們添加到數組中。這裏是一個例子..

NSArray *points = [NSArray array]; 
// wrap your point using NSValue 
NSValue *pointValue = [NSValue valueWithCGPoint:CGPointMake(x,y)]; 

[points addObject:pointValue]; 
/// ... add points 

// get your point back... 
CGPoint point = [[points objectAtIndex:index] CGPointValue]; 

button.frame = CGRectMake(point.x, point.y, button.frame.size.width, button.frame.size.height; 
+0

我有一個問題,我是動畫按鈕,並將它們放在一個地方。我想要all.i上面最近的按鈕使用sendSubViewToBack:方法發送上一個按鈕回來,但這是不工作...我希望你的或其他的建議在這 –