【问题标题】:Why does animateWithDuration fade out but not in?为什么 animateWithDuration 淡出但不淡入?
【发布时间】:2015-09-16 16:35:39
【问题描述】:

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

我有一个计数器和两个标签:

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

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

我的问题:

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

【问题讨论】:

    标签: swift uiviewanimation swift2 animatewithduration xcode7-beta2


    【解决方案1】:

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

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

    【讨论】:

    • 感谢@lcaro,我已经停了几天了。
    • 我认为每个人都至少对完成句柄@DanBeaulieu 感到困惑,到目前为止,我似乎提出了更多问题的话题。很高兴能为您提供帮助,祝您的应用顺利完成!
    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    相关资源
    最近更新 更多