帮助循环,存储&使用CGPoints

时间:2012-12-11 21:34:12

标签: ios for-loop uibutton cgpoint

我有一些UIButton被移动到不同的位置,由一个名为totalSmallButtonLocations的数组预先确定。所以我通过availableLocations,NSMutableArray,分配不同的CGPoints,并从availableLocations中删除这些点。完成后,会有一个UIView动画将按钮移动到新位置。

目前这有效,但正如您在下面看到的那样很长,(在实际项目中还有更多按钮和位置):

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方面导致了一些问题。我认为我的循环没问题,但我不知道如何在动画部分分配它,并且它引发了错误:

“从不兼容的类型ID”分配给'CGPoint'(又名'struct CGPoint'):

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 个答案:

答案 0 :(得分:2)

问题是你得到一个指向CGPoint而不是CGPoint本身的指针。问题在于[points objectAtIndex:1];你得到一个对象而不是struct CGPoint。你只需要像[[points objectAtIndex:1] CGPointValue];一样包装它,警告应该消失。