(Swift)淡入淡出标签[重复]

Posted

技术标签:

【中文标题】(Swift)淡入淡出标签[重复]【英文标题】:(Swift) Fade-In and Out a Label [duplicate] 【发布时间】:2016-08-17 04:16:49 【问题描述】:

当标签出现在屏幕上时,我正在尝试淡入淡出。目前,我只是使用

隐藏它
button.hidden = true

并使用 false 取消隐藏它。我想用淡入淡出来动画这个过程,因为这样看起来更平滑。感谢您的帮助!

这是我正在使用的出现错误的代码。 http://imgur.com/a/HI4eg

class ViewController: UIViewController 
@IBOutlet weak var yeah: UILabel!

override func viewDidLoad() 
    super.viewDidLoad()

    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: 
        self.yeah.alpha = 0.0
        , completion: 
            (finished: Bool) -> Void in

            //Once the label is completely invisible, set the text and fade it back in
            self.yeah.text = "your Text "

            // Fade in
            UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: 
                self.yeah.alpha = 1.0
                , completion: 
                    (finished: Bool) -> Void in

                    //Once the label is completely invisible, set the text and fade it back in
                    self.yeah.text = "your Text "

                    // Fade in
                    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: 
                        self.yeah.alpha = 1.0
                        , completion:nil )




            )
    // Do any additional setup after loading the view, typically from a nib.


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

【问题讨论】:

您是否正在寻找具有淡出效果的隐藏和具有淡入效果的显示?由于默认隐藏是不可动画的? 【参考方案1】:

如andrew bancroft's blog中所述

     // Move our fade out code from earlier
    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: 
        self.yourLabel.alpha = 0.0
        , completion: 
            finished in

            if finished 
                //Once the label is completely invisible, set the text and fade it back in
                self.yourLabel.text = "your Text "

                // Fade in
                UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: 
                    self.yourLabel.alpha = 1.0
                , completion: nil)
            
    )

修改后的答案

    UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: 
        self.yourLabel.alpha = 0.0
        , completion: 
            finished in

            if finished 
                //Once the label is completely invisible, set the text and fade it back in
              self.yourLabel.text = "your Text "

                // Fade in
                UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: 
                  self.yourLabel.alpha = 1.0
                    , completion: 
                        finished in

                        if finished 
                            //Once the label is completely invisible, set the text and fade it back in
                              self.yourLabel.text = "your Text "

                            // Fade in
                            UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseIn, animations: 
                                self.yourLabel.alpha = 0.0
                                , completion: nil)
                        
                )
            
    )

【讨论】:

您应该将if finished 添加到您的闭包中。完成块可能在未完成时被调用。 如果你引用某人的代码,最好先写名字作为参考。 @DipenPanchasara - 当然,我的兄弟,你能修改我按照你的方式回答的答案 @Anbu.Karthik 谢谢。 @DipenPanchasara - tanx 伙计,我跟着我的油炸了美好的一天

以上是关于(Swift)淡入淡出标签[重复]的主要内容,如果未能解决你的问题,请参考以下文章

淡入/淡出2个标签在tableview内无限循环

如何使用swift 3为按钮设置动画以淡出然后淡入

在 Swift 中为 Apple Watch 制作简单的淡入淡出动画?

重复淡入淡出效果不起作用

如何重复淡入淡出和动画?

在新闻源上重复淡入淡出