I have some UIButtons that get moved around to different locations, predetermined by an array called totalSmallButtonLocations. So I go through availableLocations, an NSMutableArray, assigning different CGPoints, and removing those points from the availableLocations. Once that is done there is a UIView animation to move the buttons to there new locations.
Currently this works, but as you can see below it is very long, (and in the actual project there are many more buttons and locations):
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;
So I’m trying to create a loop, but the CGPoint aspect is causing some problems. I think my loop is ok, but I don’t know how to assign it during the animation section, and it is throwing up the error:
“Assigning to ‘CGPoint’ (aka ‘struct CGPoint’) from incompatible type 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];
The problem is you’re getting a pointer to a CGPoint instead of the CGPoint itself. The problem is here
[points objectAtIndex:1];you’re getting an object out instead of the struct CGPoint. You simply need to wrap it like this[[points objectAtIndex:1] CGPointValue];and the warning should be gone.