26

I've always worked with Flash, and it's pretty easy to change the alpha values between one frame and another. Is there a way to do this in xcode 4? I'm animating a logo and I need the first png to disappear while the second one starts appearing. tnx!

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
Melisa D
  • 273
  • 1
  • 3
  • 12

3 Answers3

57

Alternatively to esqew's method (which is available prior to iOS 4, so you should probably use it instead if you don't plan to limit your work to just iOS 4), there is also [UIView animateWithDuration:animations:], which allows you to do the animation in a block. For example:

[UIView animateWithDuration:3.0 animations:^(void) {
    image1.alpha = 0;
    image2.alpha = 1;
}];

Fairly simple, but again, this is available only on iOS 4, so keep that in mind.

  • hi, i'm very new at this, how are you getting two images in one uUIView? – Melisa D May 20 '11 at 16:15
  • @melisa-d I just assumed that your images were UIImageView instances that were subviews of a particular view. Does that make sense? –  May 20 '11 at 23:14
  • yeah, perferct sense, I removed the array I had mentioned before and did separate UIViews, but still I can't animate them – Melisa D May 21 '11 at 20:10
  • @melisa-d Do you think you could add the code you're using now, for the animations, to the question? Might help figure out what's wrong, if anything is. –  May 21 '11 at 20:21
  • - (void)loadView { [super loadView]; [UIView animateWithDuration:5.0 animations:^(void) { introView1.alpha = 0; introView2.alpha = 1; }]; } – Melisa D May 23 '11 at 15:15
  • I don't know how I did it, but it finally worked! BUT, now I need to remove the animation so that the menu can appear. – Melisa D May 24 '11 at 22:09
  • Regards from Corleone brothers. – Michael Corleone Feb 11 '14 at 10:18
11

Other solution, fade in and fade out:

//Disappear
[UIView animateWithDuration:1.0 animations:^(void) {
       SplashImage.alpha = 1;
       SplashImage.alpha = 0;
}
completion:^(BOOL finished){
//Appear
   [UIView animateWithDuration:1.0 animations:^(void) {
      [SplashImage setImage:[UIImage imageNamed:sImageName]];
      SplashImage.alpha = 0;
      SplashImage.alpha = 1;
 }];
}];
ChavirA
  • 707
  • 8
  • 18
7

This is pretty simple actually. Place the following code where you want the animation to occur:

[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:3.0]; // you can set this to whatever you like
/* put animations to be executed here, for example: */
[image1 setAlpha:0];
[image2 setAlpha:1];
/* end animations to be executed */
[UIView commitAnimations]; // execute the animations listed above

You can read more about these methods in this document.

If you wanna work with a structure you referred to in your comment on this question:

[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:3.0]; // you can set this to whatever you like
/* put animations to be executed here, for example: */
[[introAnimation objectAtIndex:0] setAlpha:0];
[[introAnimation objectAtIndex:1] setAlpha:1];
/* end animations to be executed */
[UIView commitAnimations]; // execute the animations listed above

... should work.

esqew
  • 42,425
  • 27
  • 92
  • 132
  • what if I'm working with this: NSArray *introAnimation; introAnimation=[[NSArray alloc]initWithObjects: [UIImage imageNamed:@"intro 000.jpg"], [UIImage imageNamed:@"intro 001.jpg"], nil]; – Melisa D May 20 '11 at 16:12