Help Moving ImageViews

Discussion in 'iOS Programming' started by xcodeNewbie, Jul 11, 2011.

  1. xcodeNewbie macrumors member

    Jul 1, 2011
    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];
   = CGPointMake(,;
            [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. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Why are you releasing platform?
  3. jiminaus macrumors 65816


    Dec 16, 2010
    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. xcodeNewbie thread starter macrumors member

    Jul 1, 2011
    Still Not Working

    I tried this code:

    for (int a=0;a<54;a++) {
            UIImageView *platform = [platformArray objectAtIndex:a];
            CGPoint newCenter = CGPointMake(,;
            [[platformArray objectAtIndex:a] setCenter:newCenter];
    The images still won't move.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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. chown33 macrumors 604

    Aug 9, 2009
    Maybe the problem is in the code you haven't posted.

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

    Oct 18, 2007
    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