2012-12-11 34 views
0

我有一些UIButtons移動到不同的位置,由一個名爲totalSmallButtonLocations的數組預先確定。所以我通過availableLocations,一個NSMutableArray,分配不同的CGPoints,並從availableLocations中刪除這些點。一旦完成,就會有一個UIView動畫將按鈕移動到新的位置。幫助循環,存儲和使用CGPoint

目前這個工作,但你可以看到它下面很長,(和在實際工程中有更多的按鈕和位置):

int randomNumber; 
NSValue *val; 
CGPoint a, b, c; 
NSMutableArray *availableLocations; 
availableLocations = [NSMutableArray arrayWithArray:totalSmallButtonLocations]; 

randomNumber = arc4random_uniform(3); 
val = [availableLocations objectAtIndex:randomNumber]; 
a = [val CGPointValue]; 
[availableLocations removeObjectAtIndex:randomNumber]; 
randomNumber = arc4random_uniform(13); 
val = [availableLocations objectAtIndex:randomNumber]; 
b = [val CGPointValue]; 
[availableLocations removeObjectAtIndex:randomNumber]; 
randomNumber = arc4random_uniform(12); 
val = [availableLocations objectAtIndex:randomNumber]; 
c = [val CGPointValue]; 

[UIView animateWithDuration:0.5 animations:^{ 
    button1.center = a; 
    button2.center = b; 
    button3.center = c; 

所以我想創建一個循環,但CGPoint方面造成了一些問題。我認爲我的循環沒問題,但我不知道如何在動畫部分分配它,並且它拋出錯誤:

「從不兼容的類型指派給'CGPoint'(aka'struct CGPoint') ID「:

int randomNumber; 
NSValue *val; 
CGPoint a; 
NSMutableArray *availableLocations; 
availableLocations = [NSMutableArray arrayWithArray:totalSmallButtonLocations]; 
NSMutableArray *points = [NSMutableArray array]; 

for(int looper = 14; looper > 0; looper--) 
{ 
    randomNumber = arc4random_uniform(looper); 
    val = [availableLocations objectAtIndex:randomNumber]; 
    a = [val CGPointValue]; 
    [points addObject:[ NSValue valueWithCGPoint:a]]; 
} 
[UIView animateWithDuration:0.5 animations:^{ 
    button1.center = [points objectAtIndex:1]; 
    button2.center = [points objectAtIndex:2]; 
    button3.center = [points objectAtIndex:3]; 
+1

請勿濫用'xcode'標籤。刪除。 – 2012-12-11 21:39:34

+0

哎呦,我會牢記未來。 –

+0

感謝您牢記這一點。 – 2012-12-11 21:44:40

回答

2

問題是你得到一個指向CGPoint而不是CGPoint本身的指針。問題是這裏[points objectAtIndex:1];你得到一個對象而不是結構CGPoint。您只需簡單地將其包裝即可,並且警告應該消失。

+0

謝謝,工作的魅力。 –

+0

很高興我可以幫助:)快樂編碼。 –