Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make images appear and disappear in iOS apps?

Tags:

objective-c

I'm trying to make my app show an image (PNG) in the middle of a screen when a button is pressed and then make it fade out after a few seconds. How would I be able to do this? Also it is a small png so how can i just make it show in its original size rather then stretch it out to fit the whole screen? Any tip, suggestion or answer is greatly appreciated!

Also I am new to this site so could you please tip me or help me improve this question as some people think it is not complete. Thank you everyone for your generous answers! :)

like image 903
iHackerMe Avatar asked Sep 12 '12 11:09

iHackerMe


3 Answers

Initialize an UIImageView with an UIImage:

// Assuming MyImage.png is part of the project's resources.
UIImage *image = [UIImage imageNamed:@"MyImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Add imageView to a parent view here.
[UIView animateWithDuration:0.2f delay:3.0f options:0 
                 animations:^{imageView.alpha = 0.0;} 
                 completion:^{[imageView removeFromSuperview];}];
like image 171
DrummerB Avatar answered Oct 06 '22 00:10

DrummerB


 NSArray *animationArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"myImage.png"], nil]; 

    [NSTimer scheduledTimerWithTimeInterval:.75 target:self selector:@selector(crossfade) userInfo:nil repeats:YES];

    mainImageView.animationImages = animationArray;                
    mainImageView.animationDuration = 4.5; //mainImageView is instance of UIImageView
    mainImageView.animationRepeatCount = 0;
    [mainImageView startAnimating];

    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
    crossFade.autoreverses = YES;
    crossFade.repeatCount = 1;
    crossFade.duration = 1.0;

and the target method:

- (void) crossfade {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // user dependent transition acn be set here
    mainImageView.alpha = !mainImageView.alpha;
    [UIView commitAnimations];
}
like image 29
AppleDelegate Avatar answered Oct 06 '22 02:10

AppleDelegate


Here is the code for showing image for the few seconds on button press: in your button action add following code:

imageView.image=yourImage;
 [self performSelector:@selector(waitAndGo) withObject:nil afterDelay:5];

here is the implementation for the waitAndGo:

-(void)waitAndGo
{
    imageView.hidden=YES;
}
like image 22
V-Xtreme Avatar answered Oct 06 '22 01:10

V-Xtreme