簡體   English   中英

animateWithDuration動畫一次但不兩次

[英]animateWithDuration Animates Once But Not Twice

我在switch語句中運行了該動畫,並執行了預期的操作。 在計時器內每20秒成功調用一次動畫。 但是,動畫不會在任何時候發生,而是第一次。 在下面的代碼中,您將看到我用來嘗試突出問題的println語句。 每個println語句都會打印,但不會發生動畫。 我想念的是什么?

func popAnimation(indexNumber: Int) {

    let image = self.overlayImageView[indexNumber]
    image.hidden = false
    image.alpha = 0.0

    UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in

        println("animation ran")

        println(image.alpha)
        image.image = UIImage(named: "lowCountOverlay")
        image.alpha = 1.0


        }, completion: { (Bool) -> Void in

            UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in

                println("animation 2 ran")

                println(image.alpha)
                image.alpha = 0.0


                }, completion: { (Bool) -> Void in

                    println("animation 3 ran")
                    image.hidden = true

            })

    })

}

更新:如果我從第二個完成塊中刪除image.hidden ,它可以正常工作並反復進行動畫處理。 有趣的是,如果未隱藏它,則它應該覆蓋視圖中的其他交互式內容,但不是。 其他內容可以在模擬器中完美訪問。 UIImageView image絕對是Storyboard中的頂層。

我可能會離開,但似乎您正在從NSTimer中運行,它可能在主線程中,也可能不在主線程中。 UI更新需要在主線程中進行,嘗試將完成處理程序包裝在指向主線程的GCD調度中,如下所示,看看是否有幫助? 甚至可以將整個過程強制到主線程。

UIView.animateWithDuration(0.75, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
    // First animation
}, , completion: { (Bool) -> Void in
    dispatch_async(dispatch_get_main_queue(),{
        // second animation
    })
})

我認為您需要在其他地方尋找錯誤。 您似乎並沒有在代碼中遺漏任何東西,因為我能夠重復運行動畫而沒有任何問題。 我已經將您的代碼原樣復制到一個單獨的項目中 ,並且動畫每次都能正常工作。

嘗試更換:

let image = self.overlayImageView[indexNumber]
image.hidden = false
image.alpha = 0.0

let image = self.overlayImageView[indexNumber]
image.superview.bringSubviewToFront(image)
image.hidden = false
image.alpha = 0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM