我有一些 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];
最佳答案
问题是您得到的是指向 CGPoint 的指针,而不是 CGPoint 本身。问题出在 [points objectAtIndex:1];
您获取的是对象而不是结构 CGPoint。您只需像这样包装它 [[points objectAtIndex:1] CGPointValue];
并且警告应该消失。
关于ios - 帮助循环、存储和使用 CGPoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13828888/