【发布时间】:2017-10-04 11:34:26
【问题描述】:
我正在尝试通过创建标准动画函数来减少在整个应用程序中使用的重复代码的冗余。
我想要的结果是带有可选完成处理程序的按钮动画。这是我的代码:
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 以确保故事板上没有硬连线,并验证它不是。
我也试过用这段代码声明动画函数:
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 秒。
-
同样的结果。 Segue 会发生,动画不会。
标签: ios swift animation uibutton