ios 淡入淡出动画 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28288476/
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
Fade In and Fade out in Animation Swift
提问by PlugInBoy
I have an UIImageView with an Animation and, in the UIView, I apply a fadeIn effect, but I need to apply fade out when the UIImageView, which is animated, when is touched.
我有一个带有动画的 UIImageView,在 UIView 中,我应用了淡入淡出效果,但是当触摸动画的 UIImageView 时,我需要应用淡出。
This is what I make to fade in.
这就是我要淡入的东西。
UIView.animateWithDuration(0.5, delay: delay,
options: UIViewAnimationOptions.CurveEaseOut, animations: {
uiImageView.alpha = 1.0
}
回答by AntiStrike12
This is what I would do based on my research: (Supposing you're using storyboard)
根据我的研究,这就是我要做的:(假设您正在使用故事板)
Go to your UIImageView, and under the Attributes, check the "User Interaction Enabled" checkbox.
Drag a TapGestureRecognizer on top of the image view.
Control click on the Tap Gesture and drag to make a action on your ViewControler.swift.
Add the following code inside:
UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseOut, animations: { self.uiImageView.alpha = 0.0 }, completion: nil)
转到您的 UIImageView,然后在属性下,选中“启用用户交互”复选框。
在图像视图的顶部拖动一个 TapGestureRecognizer。
Control 单击 Tap Gesture 并拖动以在 ViewControler.swift 上执行操作。
在里面添加以下代码:
UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseOut, animations: { self.uiImageView.alpha = 0.0 }, completion: nil)
Then you're done!
然后你就完成了!
回答by Olga Konoreva
Starting from iOS 10 Apple launched new iOS Animations SDKthat is much more powerful, especially concerning timings functions and interactivity.
从 iOS 10 开始,Apple 推出了更强大的新iOS Animations SDK,尤其是在计时功能和交互性方面。
Fade out code with this approach will:
使用这种方法淡出代码将:
UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
self.uiImageView.alpha = 0.0
}).startAnimation()
To get more details about the Property Animator, take a look at iOS 10 Animations demo.
要获得有关 Property Animator 的更多详细信息,请查看iOS 10 Animations 演示。
回答by August Lin
Swift 4 . Add fade in and fade out function to UIView object
斯威夫特 4。为 UIView 对象添加淡入淡出功能
extension UIView {
func fadeIn(_ duration: TimeInterval = 0.5, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion) }
func fadeOut(_ duration: TimeInterval = 0.5, delay: TimeInterval = 1.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 0.3
}, completion: completion)
}
}
Example
例子
label.fadeIn()
label.fadeOut()
imageView.fadeOut(completion: {
(finished: Bool) -> Void in
imageView.removeFromSuperview()
})
label.fadeIn(completion: {
(finished: Bool) -> Void in
label.text = "Changed!"
})
回答by Bill
Swift 5
斯威夫特 5
This worked well for me. I wanted to animate a label with a continues fade in / fade out. I placed the label inside the "cardHeaderView".
这对我来说效果很好。我想用持续淡入/淡出为标签制作动画。我将标签放在“cardHeaderView”中。
@IBOutlet weak var cardHeaderView: UIView!
Place this inside the "viewDidAppear". I went with a delay of zero so the animation would start right away.
把它放在“viewDidAppear”里面。我的延迟为零,因此动画会立即开始。
fadeViewInThenOut(view: cardHeaderView, delay: 0)
Here is the function.
这是功能。
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 1.5
UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: {
view.alpha = 0
}, completion: nil)
}