【发布时间】:2020-04-18 12:28:38
【问题描述】:
这可能是一个愚蠢的问题,但我正在努力思考是否有更好的方法来做到这一点。如果我有 10 个 ViewController,每个都有一个不同的按钮(假设这些按钮在 Storyboard 中创建了 Segue),但我希望它们在点击时都有一个简单的效果,我会这样写:
首先,一个 UIButton 的扩展,所以它有一个处理动画的方法
extension UIButton {
func tap(){
UIButton.animate(withDuration: 0.05,
animations: { self.alpha -= 0.1 },
completion: { finish in
UIButton.animate(withDuration: 0.1, animations: {
self.alpha += 0.1
})
})
}
然后为每个 ViewController 设置一个 IBAction。
class FirstViewController: UIViewController {
@IBOutlet weak var button: UIButton!
@IBAction func buttonTouchUpInside(_ sender: Any) {
button.tap()
}
}
...
class TenthViewController: UIViewController {
@IBOutlet weak var button: UIButton!
@IBAction func buttonTouchUpInside(_ sender: Any) {
button.tap()
}
}
我想知道是否有更好的方法。以所有 UIButton 调用 tap() 的方式扩展 UIButton 的某种方式。我需要为所有这些添加一个目标吗?如果我确实使用了@IBAction,那么它会被稍后覆盖吗?
提前致谢,如果这是一个愚蠢的问题,我们深表歉意。
【问题讨论】: