Animate UIImages... How?

Discussion in 'iOS Development' started by spoonforknife, Jan 19, 2009.

  1. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    I'm looking to animate two images, as in show one (i.e. black.png), then move the view to the next (i.e. white.png), go back to black.png, and so on and so forth. I know that you'd have to set "nil" as the animation value for indefinite "rotations," but how might I go about this?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [self.view addSubview:whiteImage];
    [self.view addSubview:blackImage];
    blackImage.alpha = 0.0;

    [UIView beginAnimations:nil context:NULL];
    /*--------Your animationBlock--------------*/
    blackView.alpha = 1.0;
    [UIView endAnimations];
  3. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Is there any way to make it repeat indefinitely, because it only managed to do this once.
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    You mean like flash between images continuously? Never stopping?
  5. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Yeppers.
  6. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I haven't a clue how to make an infinite loop. Sorry

    Please Register or Log in to view images

    . I tried Googling it, but with Apples campus being on "Infinite Loop" drive/street, it didn't help much. I know you could use a 'for' loop, but it wouldn't be infinite.
  7. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    You might want to try one of these:
    Code:
    	[UIView setAnimationRepeatAutoreverses:YES];
    
    OR
    Code:
    	[UIView setAnimationRepeatCount:1e100f];
    
    This is what it says in the Documentation of "Timing, Timespaces, and CAAnimation" but I'm not sure if it applies to the UIView animations as well.
  8. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    So... combine it and ta-da this one should work:
    Code:
    [self.view addSubview:whiteImage];
    [self.view addSubview:blackImage];
    blackImage.alpha = 1.0;
    blackImage.alpha = 0.0;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:1e100f];
    blackImage.alpha = 1.0;
    whiteImage.alpha = 0.0
    [UIView endAnimations];
  9. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Thanks dude. It had this weird closing animation that went with it, and I tweaked it up and removed a few lines of extraneous code, and now it works perfect! Thanks!
  10. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Meh, you could do that, but it will eventually stop. I wouldn't though. Put your animation in a separate method. Just something simple like
    - (void)animateMyView;
    call it when you want to start your animation like so: [self animateMyView];


    Now, edit your beginAnimation like so:
    [UIView beginAnimations

    Please Register or Log in to view images

    "fadeAnimation" context:NULL]

    add an animation delegate and a animationDidStop calback within your animation block:
    Code:
    [UIView setAnimationDelegate:self]
    [UIView setAnimationDidStopSelector:@selector(fadeAnimationStopped:finished:context:)];
    Setting an animationDidStop selector will cause that selector to be called upon each time your animation stopped, whether or not is is finished. What we want to now is add that selector to your code. Add this method:
    Code:
    - (void) fadeAnimationStopped:(NSString*)animationID finished:(BOOL)finished context:(void *)context {
    	[self animateMyView];
    }
    What we are doing is recalling the animation method when your animation stops. The only problem we have now is that your animation will keep animation to black each time you call the method, even if it is already black. That's no good. To fix this, replace the one line where you have blackView.alpha = 1.0; with this
    Code:
    if (blackView.alpha > 0.50) {
    	blackView.alpha = 0.0;
    } else {
    	blackView.alpha = 1.0
    }
    This way, if for some reason, the method gets recalled before the view finishes fading in/out, you will still be covered with an animation. Lastly, we will want to add some smoothness in the event the method is recalled before the animation finishes fading in/out. Add this line to your animation block:
    [UIView setAnimationBeginsFromCurrentState:YES];


    Oh yeah, and remove all that repeat stuffs.

Share This Page