如何使图像在几秒钟内自行淡出?

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

我有一个按钮可以在 UIImageView 中显示图像。我需要做的是让这个图像在 2 秒内自行淡出,是否可以这样做?

这是我在按钮上的代码:

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

提前致谢...

试试这个代码:

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

试试这个:

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