1

我正在处理我的应用程序的一部分,在该应用程序中,我根据用户的时间进度为消息制作动画。因此,基本上是超时消息。

我有一个柜台和两个标签:

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

我有一个调用计数方法(countingUp)的 NSTimer,其中我的timerCount变量递增。当然,正如人们所期望的那样,该方法每秒都会触发一次。该countingUp方法调用一个名为updateLabels(每秒)的方法。

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

这是我的动画方法:

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

为了证明我的方法正在被调用并且我的标签确实处于 alpha 的状态,0我截取了一个屏幕截图:

在此处输入图像描述

我的问题:

我的动画完美地淡出,但它们从不淡入,它们只是出现。这是什么原因?

4

1 回答 1

2

动画发生在一个完成块中,所以基本上你同时运行两个动画,我在下面的代码中指出代码中发生了什么。

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

您可以做的是在另一个动画完成时调用一个动画:

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)
             }
    })
于 2015-06-30T03:12:39.807 回答