有没有办法将 "preprocess" 添加到 UIButton?

Is there a way way to add a "preprocess" to a UIButton?

我想在触摸 UIButton 时添加动画效果。有没有办法在 Button 被发送到它的操作之前 运行 一个函数?

我看不出您有理由将触摸事件预处理为按钮。 您可以在 ViewController,

的 ViewDidLoad 中声明动画
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 2.0
animation.fromValue = self.testView.layer.position
animation.toValue = CGPoint(x: self.testView.layer.position.x + 100, y: self.testView.layer.position.y)

这里是按钮层的位置 属性 动画,你可以修改任何你想动画的 属性 :)

现在在按钮的 IBAction 中

self.yourButton.layer.add(animation, forKey: "position")
//this is necessary, you have to set the position again once the animation complete as core animation simply removes the animate object from layer once animation finishes 
self.testView.layer.position = CGPoint(x: self.testView.layer.position.x + 100, y: self.testView.layer.position.y)
//call your method do whatever you wanted to do once button tapped
//example : lemme print my name
print("sandeep")

当您开始按下按钮时将调用以下操作。

@IBAction internal func buttonTouchDown(_ sender: AnyObject)

当您移开手指(点击按钮)时会调用这个。

@IBAction internal func buttonTouchUpInside(_ sender: AnyObject)

因此,您可以在第一个动作开始动画,然后可以结束动画并执行其余代码。除非你需要其他手势,否则应该足够了。