[英]Swift UIButton Extension Animations Override
So I have a UIButton Extension
that allows all the buttons in my app to have a nice subtle animation. 因此,我有一个UIButton Extension
,该UIButton Extension
允许我的应用程序中的所有按钮都具有精美的动画效果。 But recently I have thought of giving a few buttons a special animation but I am not sure how to give those few buttons a specific different animation other than the one that I have set universally for all the UIButtons
. 但是最近我想给几个按钮一个特殊的动画,但是除了我为所有UIButtons
通用设置的一个动画外,我不知道如何给那些按钮一个特定的不同动画。 Is there a way to override
this animation for just a few specific buttons? 有没有一种方法可以仅override
几个特定按钮来override
此动画?
Code for the extension of the button animation universally: 普遍用于扩展按钮动画的代码:
extension UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: {
self.transform = CGAffineTransform.identity
}, completion: nil)
super.touchesBegan(touches, with: event)
}
}
I think the best thing to do here is to create subclasses of UIButton
s that all do different things, instead of creating extensions of UIButton
. 我认为最好的做法是创建都执行不同功能的UIButton
的子类,而不是创建UIButton
扩展。 Extensions are supposed to add new functionality, not override existing methods, like touchesBegan
. 扩展应该添加新功能,而不是覆盖现有方法,如touchesBegan
。
You should delete your extension and write a SubtleAnimationButton
: 您应该删除扩展名并编写SubtleAnimationButton
:
class SubtleAnimationButton : UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
self.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: .allowUserInteraction, animations: {
self.transform = CGAffineTransform.identity
}, completion: nil)
super.touchesBegan(touches, with: event)
}
}
And use SubtleAnimationButton
instead. 并改用SubtleAnimationButton
。
For those buttons where you want to override the behaviour, you should use a different subclass. 对于要覆盖其行为的那些按钮,应使用其他子类。 I'll call this SpecialButton
. 我将其称为SpecialButton
。
class SpecialButton : UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// something else
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.