Reputation: 11
I just want to realize a simple animation, where a picture is faded in a few seconds after the beginning of the application and then it should be faded out and totally disappear. I've never done any animations on the iPhone so I'm stuck. When using something like image.opacity = 1.0;
like in the Developer Documentation I get error:
request for member 'opacity' in something not a structure or union.
Upvotes: 1
Views: 3999
Reputation: 6932
It seems you want a fade in and then a fade out. But time to kick in after a duration. Not just the duration of the animations themselves.
I do not normall do stuff for iOS, so forgive me if I have the code in the wrong place.
This was tested on the simulator and in a single view application.
I added a new UIview to the ViewController's UIView and linked it to its .h file as an outlet. (named it theView)
Then added a UImageView to the new UIView (theView ) and set an image for the UImageView using the attributes inspector.
Then added code to the ViewController.m file.
The main thing I want to show you is the
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
Which invokes a method of the receiver on the current thread using the default mode after a delay.
(There are similar methods that can perform on other threads…)
I have made two methods one that fades in and one that fades out.
There are two performSelectors.
The first is called when the view loads.
The second is in the fadeIn animation block. You can place this in the viewDidLoad or after the block. But I thought it would be safer to have it where it is.
here is the .m file code.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_theView.alpha = 0.0;
[self performSelector:@selector(fadeIn) withObject:nil afterDelay:2.0];
}
- (void)fadeToBlack
{
[UIView animateWithDuration:3.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{_theView.alpha = 0.0;}
completion:nil];
}
- (void)fadeIn
{
[UIView animateWithDuration:3.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{_theView.alpha = 1.0; [self performSelector:@selector(fadeToBlack) withObject:nil afterDelay:10.0];}
completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
I am expecting corrections here… as this is not really an area I am fully familiar with (ViewControllers) But its works ok in the sim.
Upvotes: 0
Reputation: 1
the solution above didn't work in my project. I had to do it like this....
**[UIView beginAnimations:@"myPic.png" context:nil];**
//You need to set the pic name
[UIView setAnimationDuration: myTimer];
_myPic.alpha = 1.0f;
_myPic.alpha = 0.0;
[UIView commitAnimations];
this is to fade out an image
regards Philipp
Upvotes: 0
Reputation: 209
Summarizing the other answers:
To fade out an image, you want code like:
// This line may not be necessary.
myImageView.alpha = 1.0f;
[UIView beginAnimations];
[UIView setAnimationDuration: 1.5];
myImageView.alpha = 0.0;
[UIView commitAnimations];
Upvotes: 0
Reputation: 6373
What you are looking for is the property "alpha", as in view.alpha. You have simply been looking for the wrong name.
Legal alpha values are from 0.0 (fully transparent) to 1.0 (fully opaque). It will do exactly what you are looking for.
Use the beginAnimations/commitAnimations paradigm referenced in the comment above.
Upvotes: 2
Reputation: 4642
To fade out an image, you want code like:
[UIView beginAnimations];
myImageView.opacity = 0.0;
[UIView commitAnimations];
You're not setting the opacity of the image itself so much as the opacity of the UIImageView
(in this example myImageView
) containing the image. You can also control the duration of the animation by adding a line:
[UIView setAnimationDuration: 1.5];
between the calls to -beginAnimations
and -commitAnimations
.
Upvotes: 4