1. UIButton
button.addTarget(self, action: #selector(leftButtonOnClick), for: .touchUpInside)
@objc private func leftButtonOnClick() {
dismiss(animated: true, completion: nil)
}
2. UIView
extension UIView {
func addOnClickListener(target: AnyObject, action: Selector) {
let gr = UITapGestureRecognizer(target: target, action: action)
gr.numberOfTapsRequired = 1
isUserInteractionEnabled = true
addGestureRecognizer(gr)
}
}
view.addOnClickListener(target: self, action: #selector(closeViewOnClick))
@objc func closeViewOnClick() {
dismiss(animated: true, completion: nil)
}
- 参考资料:
https://blog.csdn.net/ichenwin/article/details/52131140
|