如何在Swift中淡入淡出标签

cwdobuhd  于 12个月前  发布在  Swift
关注(0)|答案(6)|浏览(139)

我希望使标签淡入,在viewDidLoad(),然后在计时器后是在3淡出。我不熟悉的fadeinfadeout功能。
我该怎么做呢?

3pmvbmvn

3pmvbmvn1#

我的建议是利用Swift扩展来使代码更加模块化和易于使用。例如,如果你想在多个视图上使多个标签淡入/淡出或一个标签淡入/淡出,你必须到处传递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等)。在viewDidLoad()函数中,您可以添加:

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


现在,你可以将这个示例代码用于图像视图,标签,文本视图,滚动视图,或者UIView的任何子视图。

yhxst69z

yhxst69z2#

即使视图已经加载,当调用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)
    }
}

字符串

uxh89sit

uxh89sit3#

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()
        })

fgw7neuy

fgw7neuy4#

更新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)
        }
    }

字符串

pgvzfuti

pgvzfuti5#

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)
    }
}

字符串

cczfrluj

cczfrluj6#

  • 斯威夫特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)

}

相关问题