的UIButton动画扩展
问题描述:
我试图减少这首先创建一个标准的动画功能,在整个应用程序中使用重复的代码冗余。的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