-1

I'm trying to fade a label in and out when it appears on the screen. Currently, I'm simply hiding it using

button.hidden = true

and unhiding it by using false. I would like to animate this process with a fade-in and out as it looks much smoother this way. Appreciate the help !

Here is the code I'm using which gets an error. https://i.stack.imgur.com/gAktk.jpg

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

} }

Ash
  • 137
  • 1
  • 10
  • Are you looking for a hide with fade out, and show with fade in effect? As default hide is not animatable? – Shripada Aug 17 '16 at 04:40

1 Answers1

5

As explained in 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)
            }
    })

Modified Answer

    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)
                        }
                })
            }
    })
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143