如何使标签淡入或淡出 swift

How to make a label fade in or out in swift

我希望让标签在 viewDidLoad() 中淡入,然后在计时器到达 3 时淡出。我不熟悉 fadeinfadeout 函数。

我该怎么做?

即使视图已加载,调用 viewDidLoad 时用户也可能看不到它。这意味着当您看到动画时,您可能会发现它似乎已经开始了。要解决此问题,您需要在 viewDidAppear 中开始动画。

至于 fadeInfadeOut 函数 - 它们不存在。你需要自己写。值得庆幸的是,这很容易做到。像下面这样的东西可能就足够了。

func fadeViewInThenOut(view : UIView, delay: NSTimeInterval) {

    let animationDuration = 0.25

    // Fade in the view
    UIView.animateWithDuration(animationDuration, animations: { () -> Void in
        view.alpha = 1
        }) { (Bool) -> Void in

            // After the animation completes, fade out the view after a delay

            UIView.animateWithDuration(animationDuration, delay: delay, options: .CurveEaseInOut, animations: { () -> Void in
                view.alpha = 0
                },
                completion: nil)
    }
}

我的建议是利用 Swift 扩展使代码更加模块化和易于使用。例如,如果你想在多个视图上制作多个标签 fadein/out 或一个标签淡出 in/out,你将不得不在所有地方传递 animateWithDuration 方法,这可能很麻烦。一个更简洁的替代方法是创建一个名为 UIView.swift 的文件(我们的 UIView 扩展名)。将以下代码添加到此文件中:

import Foundation

extension UIView {


func fadeIn(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
    UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.alpha = 1.0
        }, completion: completion)  }

func fadeOut(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 3.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) {
    UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.alpha = 0.0
        }, completion: completion)
}

}

现在您可以向 UIView 的任何子项(例如 UILabel、UIImage 等)添加 fadeIn/fadeout 功能。在 viewDidLoad() 函数中,您可以添加:

self.statusLabel.alpha = 0
self.statusLabel.text = "Sample Text Here"
self.myLabel.fadeIn(completion: {
        (finished: Bool) -> Void in
        self.myLabel.fadeOut()
        })

现在,您可以将此示例代码用于图像视图、标签、文本视图、滚动视图或 UIView 的任何子视图。我希望这有帮助。

更新 Swift 3.1 - @Andy 代码

func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
        let animationDuration = 0.25

        // Fade in the view
        UIView.animate(withDuration: animationDuration, animations: { () -> Void in
            view.alpha = 1
        }) { (Bool) -> Void in

            // After the animation completes, fade out the view after a delay

            UIView.animate(withDuration: animationDuration, delay: delay, options: [.curveEaseOut], animations: { () -> Void in
                view.alpha = 0
            }, completion: nil)
        }
    }

已更新 Swift 3 的答案 - 使用扩展程序

extension UIView {
    func fadeIn(duration: TimeInterval = 1.0, 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 = 1.0, delay: TimeInterval = 3.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
        UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
            self.alpha = 0.0
        }, completion: completion)
    }
}

用法:

self.statusLabel.alpha = 0
self.statusLabel.text = "Sample Text Here"
self.myLabel.fadeIn(completion: {
        (finished: Bool) -> Void in
        self.myLabel.fadeOut()
        })

SWIFT 4.2

extension UIView {
    func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
        UIView.animate(withDuration: duration, delay: delay, options: .curveEaseIn, animations: {
            self.alpha = 1.0
        }, completion: completion)
    }

    func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 3.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
        UIView.animate(withDuration: duration, delay: delay, options: .curveEaseIn, animations: {
            self.alpha = 0.0
        }, completion: completion)
    }
}

Swift 5

这对我来说效果很好。我把标签放在 "cardHeaderView".

里面
@IBOutlet weak var cardHeaderView: UIView!

将其放在 "viewDidAppear" 中。我的延迟为零,因此动画会立即开始。

fadeViewInThenOut(view: cardHeaderView, delay: 0)

这是函数。

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)

}