2017-10-04 122 views
0

我試圖減少這首先創建一個標準的動畫功能,在整個應用程序中使用重複的代碼冗餘。的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

嘗試給一些延遲說0.3秒。 – ankit

+0

同樣的結果。賽格發生,動畫不會。 – Adrian

回答

3

那麼它不是動畫,因爲我覺得你忘了給初始轉換值按鈕調用擴展方法之前。

myButton.transform = CGAffineTransform(scaleX: 1.2, y: 1.2) 
myButton.animate(duration: 0.25) { _ in 
    self.performSegue(withIdentifier: "mySequeIdentifier", sender: sender) 
} 
+0

哦,男孩......我的一部分很愚蠢的疏忽。謝謝。 – Adrian

+0

是的,有時候會發生!!!!享受編碼;) – ankit