How to make an image fade out by itself in a few seconds?

190 views Asked by At

i got a button to show an image in the UIImageView. what i need to do is to get this image to fade out by itself in 2 seconds, is it possible to do so?

here is my code on the button:

    - (IBAction)A {
    UIImage *Img = [UIImage imageNamed:@"AString.png"];
    [ImageView setImage:Img];
}

thank you in advance...

2

There are 2 answers

1
Massimo Polimeni On BEST ANSWER

Try this:

- (IBAction)A 
{
     UIImage *Img = [UIImage imageNamed:@"AString.png"];
     [ImageView setImage:Img];

     Img.hidden = NO;
     Img.alpha = 1.0f;

     [UIView animateWithDuration:2 delay:0 options:0 animations:^{
         Img.alpha = 0.0f;
     } completion:^(BOOL finished) {
         Img.hidden = YES;
     }];
}
1
Medo On

Try this code :

yourImageView.hidden = YES;    
[UIView transitionWithView:yourImageView
                      duration:2
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        [self.view layoutIfNeeded]; // Called on parent view
                    }
                    completion:NULL];