Melisa D
Melisa D

Reputation: 273

animation alpha change

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!

Upvotes: 26

Views: 33790

Answers (3)

ChavirA
ChavirA

Reputation: 707

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;
 }];
}];

Upvotes: 11

esqew
esqew

Reputation: 44699

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.

Upvotes: 7

user457812
user457812

Reputation:

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.

Upvotes: 58

Related Questions