(斯威夫特)淡入和退出标签

时间:2016-08-17 04:16:49

标签: ios swift

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

隐藏它
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 个答案:

答案 0 :(得分:5)

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