1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Help Moving ImageViews

Discussion in 'iPhone/iPad Programming' started by xcodeNewbie, Jul 11, 2011.

  1. macrumors member

    #1
    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.
     
  2. Moderator

    dejo

    Staff Member

    #2
    Why are you releasing platform?
     
  3. macrumors 65816

    jiminaus

    #3
    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.
     
  4. macrumors member

    #4
    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.
     
  5. macrumors 68030

    PhoneyDeveloper

    #5
    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?
     
  6. macrumors 603

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

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

    #7
    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?
     

Share This Page