5

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

Popeye
  • 11,839
  • 9
  • 58
  • 91
iHackerMe
  • 581
  • 2
  • 5
  • 17
  • 1
    Well, to make it show in the middle of the screen you *could* position a UIImageView there. Making it fade would probably involve using the animation stuff, but I've never attempted this. – Hot Licks Sep 12 '12 at 11:37
  • 2
    (Have you considered actually looking at the documentation for UIImageView and UIView?) – Hot Licks Sep 12 '12 at 11:40
  • I have but it did not help me and confused me even more :( – iHackerMe Sep 12 '12 at 12:21

3 Answers3

10

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];}];
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • Consider next time just giving a few hints. – Hot Licks Sep 12 '12 at 11:41
  • 2
    I'll give an answer here for the second part of the question (displaying the image in normal size), as this is the best answer. You should set the *contentMode* property of the UIImageView to *UIViewContentModeCenter*. You can do this by selecting the UIImageView in Interface Builder (or in your storyboard) and setting the property, or programmatically : **imageView.contentMode = UIViewContentModeCenter;**. – rdurand Sep 12 '12 at 11:48
  • 1
    Thanks a lot everyone for your quick and helpful answers! I really love stackoverflow :) – iHackerMe Sep 12 '12 at 12:18
3
 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];
}
Pradip Sutariya
  • 257
  • 2
  • 16
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • 2
    Consider next time just giving a few hints. – Hot Licks Sep 12 '12 at 11:42
  • Man you guys totally rock! Thanks a lot for your answers and yes next time I will write a more detailed question :) Sorry I'm new and learning about posting proper questions. Any suggestions on how I could have made this better? – iHackerMe Sep 12 '12 at 12:19
  • Hey @AppleDelegate When i try to compile using your code I get this ‘mainImageView’ was not declared in this scope – iHackerMe Sep 12 '12 at 13:01
3

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;
}
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79