```
let btn = UIButton.init(frame: CGRect.init(x: 0, y: 100, width: 300, height: 100))
btn.backgroundColor = UIColor.gray
/// 按下去
btn.addTarget(self, action: #selector(btnSel(sender:)), for: UIControlEvents.touchUpInside)
/// 离开
btn.addTarget(self, action: #selector(btnSel2(sender:)), for: UIControlEvents.touchDown)
view.addSubview(btn)
@objc private func btnSel(sender : UIButton) {
sender.backgroundColor = UIColor.gray
print(sender.state)
}
@objc private func btnSel2(sender : UIButton) {
sender.backgroundColor = UIColor.yellow
print(sender.state)
}