我試圖減少這首先創建一個標準的動畫功能,在整個應用程序中使用重複的代碼冗餘。的UIButton動畫擴展
我期望的結果是一個可選的完成處理程序的按鈕的動畫。這裏是我的代碼:
extension UIButton {
func animate(duration: TimeInterval, completion: ((Bool) -> Swift.Void)?) {
UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6.0, options: [], animations: {
self.transform = .identity
}, completion: completion)
}
}
在MyViewController
,當我打電話這樣的動畫法,SEGUE發生,但動畫並不:
myButton.animate(duration: 0.25) { _ in
self.performSegue(withIdentifier: "mySequeIdentifier", sender: sender)
}
我註釋掉self.performSegue
以確保SEGUE是在故事板上沒有硬連線,並驗證它不是。
我也嘗試宣告動畫功能,此代碼:
func animate(duration: TimeInterval, completion: @escaping (Bool)->Void?) {
UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6.0, options: .allowUserInteraction, animations: {
self.transform = .identity
}, completion: completion as? (Bool) -> Void)
}
使用的代碼,沒有任何反應。甚至沒有崩潰。
謝謝您的閱讀。我歡迎你的建議:我的錯誤在哪裏。
嘗試給一些延遲說0.3秒。 – ankit
同樣的結果。賽格發生,動畫不會。 – Adrian