2017-02-13 62 views
0

在使用swift 3的Xcode 8中,我有2個函數。當「HideButton」函數被調用時,它會執行正確的淡出動畫,但是當調用「ShowButton」函數時,淡入動畫不會發生。 「ShowButton」動畫功能有什麼問題,我該如何解決?我怎樣才能使一個按鈕淡出動畫,然後使用swift淡入功能3

func HideButton() { 

    UIView.animate(withDuration: 0.2, delay: 0, animations: { 
    self.MainButton.alpha = 0 
    }, completion: { finished in 
    self.MainButton.isHidden = true 
    }) 

    Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.ShowButton), userInfo: nil, repeats: false) 

} 

func ShowButton() { 

    UIView.animate(withDuration: 0.2, delay: 0, animations: { 
    self.MainButton.alpha = 1 
    }, completion: { finished in 
    self.MainButton.isHidden = false 
    }) 

    Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.HideButton), userInfo: nil, repeats: false) 

} 
+0

[動畫顯示按鈕]的可能重複(http://stackoverflow.com/questions/41466952/animated-display-button) – matt

+0

刪除提及'isHidden'的所有行。完成。 – matt

回答

0

hideButton函數中的isHidden屬性設置爲true。因此,這會限制按鈕的功能並阻止您嘗試在showButton中呈現的視覺更改。因此,您需要使該按鈕在動畫之前不隱藏,而不是在完成處理程序中隱藏。

事情是這樣的:

func hideButton() { 

UIView.animate(withDuration: 0.2, delay: 0, animations: { 
self.MainButton.alpha = 0 
}, completion: { finished in 
self.MainButton.isHidden = true 
}) 

Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.ShowButton), userInfo: nil, repeats: false) 

} 

func showButton() { 
self.MainButton.isHidden = false 

UIView.animate(withDuration: 0.2, delay: 0, animations: { 
self.MainButton.alpha = 1 
}, completion: { finished in 

}) 

Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.HideButton), userInfo: nil, repeats: false) 

} 

因爲阿爾法將在showButton動畫你仍然會得到儘管是否隱藏屬性是假的動畫發生之前所需的視覺效果的開頭爲零。請注意,我將您的函數重命名爲保留約定(funcs應該是小寫)!

+0

謝謝,它工作! –

+0

沒問題!乾杯! –