Swift 之添加点击事件
文章目录1. UIButton2. UIView1. UIButton/// 添加点击事件button.addTarget(self, action: #selector(leftButtonOnClick), for: .touchUpInside)/// 点击事件@objc private func leftButtonOnClick() {dismiss(animated: true, co
·
文章目录
1. UIButton
/// 添加点击事件
button.addTarget(self, action: #selector(leftButtonOnClick), for: .touchUpInside)
/// 点击事件
@objc private func leftButtonOnClick() {
dismiss(animated: true, completion: nil)
}
2. UIView
- 扩展方法:
extension UIView {
/// 为 view 添加点击事件
/// - Parameters:
/// - target: 点击事件的目标
/// - action: 点击事件的响应
func addOnClickListener(target: AnyObject, action: Selector) {
/// 创建手势
let gr = UITapGestureRecognizer(target: target, action: action)
/// 设置手指🤌个数
gr.numberOfTapsRequired = 1
/// 将 UIView 设置为可交互的
isUserInteractionEnabled = true
/// 将手势添加到 UIView
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
更多推荐
已为社区贡献2条内容
所有评论(0)