Tan*_*yem 2 uibutton uiviewanimation ios swift
是否可以创建一个带有触摸事件动画的自定义 UIButton 类,每次用户触摸按钮时都会自动调用该动画?
import UIKit
class AnimatedButton: UIButton {
@objc private func buttonClicked(sender: UIButton) {
UIView.animate(withDuration: 0.5,
delay: 1.0,
usingSpringWithDamping: 1.0,
initialSpringVelocity: 0.2,
options: .curveEaseOut,
animations: {
//do animation
sender.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
},
completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
所以我不想在必须调用 button.buttonTouched() 的 ViewController 中创建一个动作。每次我在所有 UIButton 中使用此类时,这都应该自动发生。
有没有可能做这样的事情?
像这样创建按钮的自定义类
class CustomButton:UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
configeBtn()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configeBtn()
}
func configeBtn() {
self.addTarget(self, action: #selector(btnClicked(_:)), for: .touchUpInside)
}
@objc func btnClicked (_ sender:UIButton) {
// animate here
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2028 次 |
最近记录: |