2秒后显示和淡化UIImageView

时间:2021-06-21 15:30:22

I am working on a notification system in my iPhone game and want an image to pop up on the screen and automatically fade after 2 seconds.

我正在我的iPhone游戏中使用通知系统,并希望在屏幕上弹出一个图像并在2秒后自动淡出。

  1. User clicks a button that calls the method "popupImage"
  2. 用户单击调用方法“popupImage”的按钮
  3. An image appears on the screen in a designated spot, it doesn't need to fade in
  4. 屏幕上会出现一个指定位置的图像,不需要淡入
  5. The image fades out by itself after being on the screen for 2 seconds.
  6. 在屏幕上显示2秒钟后,图像会自动淡出。

Is there any way of doing this? Thanks ahead of time.

有没有办法做到这一点?提前谢谢。

4 个解决方案

#1


53  

Use UIView dedicated methods for that.

使用UIView专用方法。

So imagine you already have your UIImageView ready, already created and added to the main view, but simply hidden. Your method simply need to make it visible, and start an animation 2 seconds later to fade it out, by animating its "alpha" property from 1.0 to 0.0 (during a 0.5s animation):

所以想象一下你已经准备好你的UIImageView,已经创建并添加到主视图中,但只是隐藏了。你的方法只需要让它可见,并在2秒后开始动画淡出它,通过将其“alpha”属性设置为1.0到0.0(在0.5s动画期间):

-(IBAction)popupImage
{
    imageView.hidden = NO;
    imageView.alpha = 1.0f;
    // Then fades it away after 2 seconds (the cross-fade animation will take 0.5s)
    [UIView animateWithDuration:0.5 delay:2.0 options:0 animations:^{
         // Animate the alpha value of your imageView from 1.0 to 0.0 here
         imageView.alpha = 0.0f;
     } completion:^(BOOL finished) {
         // Once the animation is completed and the alpha has gone to 0.0, hide the view for good
         imageView.hidden = YES;
     }];
}

Simple as that!

就那么简单!

#2


10  

In Swift and XCode 6

在Swift和XCode 6中

self.overlay.hidden = false
UIView.animateWithDuration(2, delay:5, options:UIViewAnimationOptions.TransitionFlipFromTop, animations: {
    self.overlay.alpha = 0
    }, completion: { finished in
    self.overlay.hidden = true
})

where overlay is the outlet for my image.

覆盖是我的图像的出口。

#3


2  

Swift 3 version of @AliSoftware's answer

Swift 3版@AliSoftware的回答

imageView.isHidden = false
imageView.alpha = 1.0

UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: {

            self.imageView.alpha = 0.0

        }) { (finished: Bool) in

            self.imageView.isHidden = true
        }

#4


0  

Yes there is. Take a look at UIView block based animation here. And google for an example.

就在这里。在这里看看基于UIView块的动画。谷歌举个例子。

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

You can also start a timer

你也可以启动一个计时器

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

#1


53  

Use UIView dedicated methods for that.

使用UIView专用方法。

So imagine you already have your UIImageView ready, already created and added to the main view, but simply hidden. Your method simply need to make it visible, and start an animation 2 seconds later to fade it out, by animating its "alpha" property from 1.0 to 0.0 (during a 0.5s animation):

所以想象一下你已经准备好你的UIImageView,已经创建并添加到主视图中,但只是隐藏了。你的方法只需要让它可见,并在2秒后开始动画淡出它,通过将其“alpha”属性设置为1.0到0.0(在0.5s动画期间):

-(IBAction)popupImage
{
    imageView.hidden = NO;
    imageView.alpha = 1.0f;
    // Then fades it away after 2 seconds (the cross-fade animation will take 0.5s)
    [UIView animateWithDuration:0.5 delay:2.0 options:0 animations:^{
         // Animate the alpha value of your imageView from 1.0 to 0.0 here
         imageView.alpha = 0.0f;
     } completion:^(BOOL finished) {
         // Once the animation is completed and the alpha has gone to 0.0, hide the view for good
         imageView.hidden = YES;
     }];
}

Simple as that!

就那么简单!

#2


10  

In Swift and XCode 6

在Swift和XCode 6中

self.overlay.hidden = false
UIView.animateWithDuration(2, delay:5, options:UIViewAnimationOptions.TransitionFlipFromTop, animations: {
    self.overlay.alpha = 0
    }, completion: { finished in
    self.overlay.hidden = true
})

where overlay is the outlet for my image.

覆盖是我的图像的出口。

#3


2  

Swift 3 version of @AliSoftware's answer

Swift 3版@AliSoftware的回答

imageView.isHidden = false
imageView.alpha = 1.0

UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: {

            self.imageView.alpha = 0.0

        }) { (finished: Bool) in

            self.imageView.isHidden = true
        }

#4


0  

Yes there is. Take a look at UIView block based animation here. And google for an example.

就在这里。在这里看看基于UIView块的动画。谷歌举个例子。

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations

You can also start a timer

你也可以启动一个计时器

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats