【发布时间】:2019-11-28 18:47:14
【问题描述】:
我的 Swift 按钮中有以下扩展:
extension UIButton {
func backgroundChange() {
let colorAnimation = CABasicAnimation(keyPath: "backgroundColor")
colorAnimation.duration = 0.3
colorAnimation.isAdditive = true
colorAnimation.fromValue = UIColor.black.cgColor
colorAnimation.toValue = self.layer.backgroundColor
colorAnimation.repeatCount = 1
colorAnimation.autoreverses = true
layer.add(colorAnimation, forKey: nil)
}
}
这基本上只是触发按钮中的“闪光”动画。问题是,每次我希望发生这种情况时,我都必须手动调用此方法。
我应该对此进行什么修改,以便在您点击按钮时对班级本身发生这种情况? 或者,换一种说法,我怎样才能覆盖所有 UIButtons 通用的“onClick”方法,以便在点击时它们都会自动闪烁?
编辑:用户已将此问题标记为重复。但它不是。他们链接的问题根本没有回答我的问题。
我找到了正确的方法。这是要走的路:
override func touchesBegan(_ touches: Set<UITouch>, with: UIEvent?){
backgroundChange()
super.touchesBegan(touches as! Set<UITouch>, with: with)
}
【问题讨论】:
标签: ios swift user-interface uibutton