ios 2 秒后显示和淡入 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12467454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show and Fade UIImageView after 2 Seconds
提问by felix_xiao
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 秒后自动淡入淡出。
- User clicks a button that calls the method "popupImage"
- An image appears on the screen in a designated spot, it doesn't need to fade in
- The image fades out by itself after being on the screen for 2 seconds.
- 用户单击调用“popupImage”方法的按钮
- 图像出现在屏幕上的指定位置,不需要淡入
- 图像在屏幕上停留 2 秒后自行淡出。
Is there any way of doing this? Thanks ahead of time.
有没有办法做到这一点?提前致谢。
回答by AliSoftware
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.5 秒动画期间):
-(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!
就那么简单!
回答by Joseph Selvaraj
Swift 2
斯威夫特 2
self.overlay.hidden = false
UIView.animateWithDuration(2, delay: 5, options: UIViewAnimationOptions.TransitionFlipFromTop, animations: {
self.overlay.alpha = 0
}, completion: { finished in
self.overlay.hidden = true
})
Swift 3, 4, 5
斯威夫特 3、4、5
self.overlay.isHidden = false
UIView.animate(withDuration: 2, delay: 5, options: UIView.AnimationOptions.transitionFlipFromTop, animations: {
self.overlay.alpha = 0
}, completion: { finished in
self.overlay.isHidden = true
})
Where overlay
is the outlet for my image.
overlay
我的形象的出口在哪里。
回答by iUser
Swift 3 version of @AliSoftware's answer
@AliSoftware答案的Swift 3 版本
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
}
回答by ohr
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