2017-10-04 82 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