swiftuiviewanimationswift2animatewithdurationxcode7-beta2

Why does animateWithDuration fade out but not in?


I'm working on a portion of my application where I animate messages based on a users progress through time. So, essentially timed out messages.

I have a counter and two labels:

var timer = NSTimer()
var timerCount = 0
@IBOutlet weak var bannerLabel: UILabel!
@IBOutlet weak var messageLabel: UILabel!

I have an NSTimer that's calling a count method (countingUp) where my timerCount variable is incremented. Of course the method fires every second as one would expect. The countingUp method calls a method called updateLabels (each second).

func countingUp() {
    // other irrelevant stuff
    updateLabels()
    timerCount++
}

func updateLabels() {           
   if timerCount == 1 {          
       animateMessage(messageLabel, delay: 7.0)
       animateBanner(bannerLabel, delay: 7.0)
       bannerLabel.text = "Message 1"
       messageLabel.text = "Blah Blah"
   }       
   // quite a few more of these conditions, though
   // I use a switch in my app.   
}

And here are my animation methods:

    func animateBanner(banner: UILabel, delay: Double){  
        UIView.animateWithDuration(1.2, animations: {
            banner.alpha = 1.0
        })

        if delay > 0.0 {
            UIView.animateWithDuration(3, delay: delay, options: [], animations: {
            banner.alpha = 0.1

            }, completion: nil)
         }
    }

    func animateMessage(label: UILabel, delay: Double){
        label.alpha = 0.0
        UIView.animateWithDuration(1.2, animations: {
            label.center.y -= 20
            label.alpha = 1.0
        })

        UIView.animateWithDuration(1.2, delay: delay, options: [], animations: {
            label.alpha = 0.1

            }, completion: nil)
    }

To prove that my method is being called and that my label is indeed at alpha of 0 I've taken a screen shot:

enter image description here

My Question:

My animations fade out perfectly, but they never fade in, they just appear. What is the reason for this?


Solution

  • The animation happens in a completion block, so basically you are running two animations at the same time, I indicate what is happening in the code in the code below.

    func animateMessage(label: UILabel, delay: Double){
            label.alpha = 0.0 
            //1 - Start animation 1
            UIView.animateWithDuration(1.2, animations: {
                //3 - Finish animation 1
                label.center.y -= 20
                label.alpha = 1.0
            })
            //2 - Start animation 2
            UIView.animateWithDuration(1.2, delay: delay, options: [], animations: {
                 //4 - Finish animation 2
                 label.alpha = 0.1
                }, completion: nil)
        }
    

    What you can do is to call one animation on the completion of the other animation:

    func animateMessage(label: UILabel, delay: Double){
            label.alpha = 0.0 
            UIView.animateWithDuration(1.2, delay: delay, options: [], animations: {
                label.center.y -= 20
                label.alpha = 1.0
            }, completion:{finished in
                if (finished) {
                    UIView.animateWithDuration(1.2, delay: delay, options: [], animations: {
                         label.alpha = 0.1
                        }, completion: nil)
                 }
        })