iHackerMe
iHackerMe

Reputation: 591

How to make images appear and disappear in iOS apps?

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! :)

Upvotes: 5

Views: 4210

Answers (3)

AppleDelegate
AppleDelegate

Reputation: 4249

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

Upvotes: 3

V-Xtreme
V-Xtreme

Reputation: 7333

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

Upvotes: 3

DrummerB
DrummerB

Reputation: 40211

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

Upvotes: 10

Related Questions