如何禁用标准 UIButton 触摸事件
Posted
技术标签:
【中文标题】如何禁用标准 UIButton 触摸事件【英文标题】:How to disable standard UIButton touch event 【发布时间】:2017-07-25 23:22:14 【问题描述】:在我的应用程序中,我添加了一个 UIButton,但不是让 UIButton 的标准触摸事件变暗然后在触摸事件结束时变为标准颜色,而是添加一个动画,当您单击 UIButton 按钮时变小,然后当触摸事件结束时,UIButton 在触摸事件结束时恢复到常规大小。到目前为止,我已经输入了这段代码,但它似乎不起作用。请告诉我如何完成这个动画。
// whatsTheGame is the UIButton I have
@IBAction func whatsTheGame(_ sender: UIButton)
logo.isHighlighted = false
UIView.animate(withDuration: 0.3)
if let centerLogo = self.logo
centerLogo.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?)
super.touchesCancelled(touches, with: event)
UIView.animate(withDuration: 0.3)
if let centerLogo = self.logo
centerLogo.transform = CGAffineTransform(scaleX: 1, y: 1)
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
super.touchesEnded(touches, with: event)
UIView.animate(withDuration: 0.3)
if let centerLogo = self.logo
centerLogo.transform = CGAffineTransform(scaleX: 1, y: 1)
【问题讨论】:
【参考方案1】:添加 Touch Up Inside 和 Touch Down 事件
@IBAction func onTouchUpInside(_ sender: Any)
let button = sender as! UIButton
UIView.animate(withDuration: 0.3)
button.transform = CGAffineTransform(scaleX: 1, y: 1)
@IBAction func onTouchDown(_ sender: Any)
let button = sender as! UIButton
UIView.animate(withDuration: 0.3)
button.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
【讨论】:
您可能还需要考虑添加touchDragOut(_:)
、touchDragIn(_:)
以获得更标准的行为:-)以上是关于如何禁用标准 UIButton 触摸事件的主要内容,如果未能解决你的问题,请参考以下文章