文章目录


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
Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐