Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xcodeNewbie

macrumors member
Original poster
Jul 1, 2011
65
0
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:
Code:
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Code:
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...
        [COLOR="Red"][platform release];[/COLOR]
    }

Why are you releasing platform?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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:
Code:
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

macrumors member
Original poster
Jul 1, 2011
65
0
Still Not Working

I tried this code:

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

macrumors 68040
Sep 2, 2008
3,114
93
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

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
I tried this code:

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

macrumors regular
Oct 18, 2007
108
0
Chicago-area
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.