如何使标签在swift中淡入或淡出 [英] How to make a label fade in or out in swift

查看:151
本文介绍了如何使标签在swift中淡入或淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 viewDidLoad()中使标签淡入,然后在计时器淡出3之后。我不熟悉 fadein fadeout 函数。

I am looking to make a label fade in, in viewDidLoad(), and then after a timer is at 3 fade out. I am not familiar with the fadein or fadeout functions.

我将如何进行此操作?

推荐答案

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

Even though the view has loaded, it may not be visible to the user when viewDidLoad is called. This means you might find your animations appears to have already started when you witness it. To overcome this issue, you'll want to start your animation in viewDidAppear instead.

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

As for the fadeIn and fadeOut functions - they don't exist. You'll need to write them yourself. Thankfully, it's very easy to do this. Something like the below might be good enough.

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中淡入或淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆