我有一些 UIButtons 可以移动到不同的位置,由一个名为 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];