PDA

View Full Version : Help Moving ImageViews




xcodeNewbie
Jul 11, 2011, 05:36 PM
In my app I have an array of image views. I want to move each image in the array up the screen by one pixel at a time. Here's is the code for how I'm trying to do it:
for (int a=0;a<54;a++) {
UIImageView *platform = [platformArray objectAtIndex:a];
platform.center = CGPointMake(platform.center.x,platform.center.y-1);
[platformArray replaceObjectAtIndex:a withObject:platform]; // Not really sure if this is necessary...
[platform release];
}
All of the image views were created and added to my super view in viewDidLoad. However when the program starts all the images are just stationary and never move.



dejo
Jul 11, 2011, 05:43 PM
for (int a=0;a<54;a++) {
UIImageView *platform = [platformArray objectAtIndex:a];
platform.center = CGPointMake(platform.center.x,platform.center.y-1);
[platformArray replaceObjectAtIndex:a withObject:platform]; // Not really sure if this is necessary...
[platform release];
}

Why are you releasing platform?

jiminaus
Jul 11, 2011, 05:46 PM
In my app I have an array of image views. I want to move each image in the array up the screen by one pixel at a time. Here's is the code for how I'm trying to do it:
for (int a=0;a<54;a++) {
UIImageView *platform = [platformArray objectAtIndex:a];
platform.center = CGPointMake(platform.center.x,platform.center.y-1);
[platformArray replaceObjectAtIndex:a withObject:platform]; // Not really sure if this is necessary...
[platform release];
}
All of the image views were created and added to my super view in viewDidLoad. However when the program starts all the images are just stationary and never move.

First thoughts on this code.

You don't need to replace the object in the array after you change it. objectAtIndex: doesn't return a copy of the object in the array, only a pointer to it. In fact the array doesn't have objects in, it holds pointers to them.

(Re-)read the memory management guide. You do not own the object pointed to by platform because you didn't get the pointer by sending alloc or get, and you haven't retained it. So it's wrong to release it at the end of the loop.

xcodeNewbie
Jul 11, 2011, 08:11 PM
I tried this code:

for (int a=0;a<54;a++) {
UIImageView *platform = [platformArray objectAtIndex:a];
CGPoint newCenter = CGPointMake(platform.center.x,platform.center.y-1);
[[platformArray objectAtIndex:a] setCenter:newCenter];
}

The images still won't move.

PhoneyDeveloper
Jul 11, 2011, 09:59 PM
When, Where, (Why)? is this code being called? Are you even sure that it IS being called? And Why didn't you mention that already?

chown33
Jul 12, 2011, 10:45 AM
I tried this code:

for (int a=0;a<54;a++) {
UIImageView *platform = [platformArray objectAtIndex:a];
CGPoint newCenter = CGPointMake(platform.center.x,platform.center.y-1);
[[platformArray objectAtIndex:a] setCenter:newCenter];
}

The images still won't move.

Maybe the problem is in the code you haven't posted.

The way to change that is to post more of your code.

RonC
Jul 12, 2011, 11:22 AM
The images still won't move.

A viewable object (subclass of UIView) is not going to move/update on the screen until a) you tell the view that it needs to redisplay itself (and if you move it, you may need to tell it to re-layout itself), and b) you exit back to the main loop. Your code stays in a loop and moves the center 1 pixel. When does the system get a chance to redisplay/relayout the view, and how will it know it needs to be redisplayed/relayed-out?