如果我们点击背景中的任何地方,除了在 swift 中点击 popView 之外,如何删除 popView

How to remove popView if we tap anywhere in the background except tapping in popView in swift

我在 ViewController 中创建了一个带有文本字段和按钮的 popView。如果我单击按钮然后 popView 出现,我可以在文本字段中输入文本并提交工作,如果我点击视图中的任何地方我也可以删除 popView,但在这里我想要如果我点击 popView 中的任何地方我不想关闭 popView,请在代码中帮助我。

这是我的代码:

 import UIKit

 class PopUPViewController: UIViewController {

@IBOutlet weak var popView: UIView!
@IBOutlet weak var inputField: UITextField!
@IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    popView.isHidden = true
    // Do any additional setup after loading the view.
}

@IBAction func butnAct(_ sender: Any) {
    view?.backgroundColor = UIColor(white: 1, alpha: 0.9)
    popView.isHidden = false
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopUPViewController.dismissView))
    view.addGestureRecognizer(tap)
}

@objc func dismissView() {
    self.popView.isHidden = true
    view?.backgroundColor = .white
}

@IBAction func sendButton(_ sender: Any) {
    self.textLabel.text = inputField.text
}
}

在我的代码中,如果我点击视图中的任何地方,popView 将被删除,即使我点击 popView 也会删除它,我不需要它,如果我点击 popView,则不需要删除 popView。

请帮我写代码

您可以 override touchesBegan 方法,即 triggerednew touchdetected 时 [=15] =] 或 window。通过使用此 method,您可以检查特定的 view 是否被触摸。

这样试试

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
     let touch = touches.first
     if touch?.view != self.popView {
        dismissView()
    }
}

func dismissView() {
    self.popView.isHidden = true
    view?.backgroundColor = .white
}

这不是我设计的方式,但要解决您面临的问题,您需要调整 dismissView 方法,以便它仅在点击位于 popView 之外时关闭视图。

为此修改您的选择器以将发件人(UITapGestureRecogniser)作为参数包括在内:

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PopUPViewController.dismissView(_:)))

然后在您的函数中接受该参数并测试点击是否在您的视图内,如果是,请不要关闭视图:

@objc func dismissView(_ sender: UITapGestureRegognizer) {
  let tapPoint = sender.location(in: self.popView)
  if self.popView.point(inside: tapPoint, with: nil)) == false {
    self.popView.isHidden = true
    view?.backgroundColor = .white
  }
}

您的 Popup 视图位于 viewcontroller 的父视图内,这就是为什么在点击 popview 时您的 popview 也会被隐藏。 因此,为了避免只在后台添加一个视图并将其命名为 bgView 或任何您想要的名称并将其替换为视图。它会很好地工作。

代码:

@IBOutlet weak var bgView: UIView!//Add this new outlet
@IBOutlet weak var popView: UIView!
@IBOutlet weak var inputField: UITextField!
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var submitButton: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    popView.isHidden = true

}



@IBAction func butnAct(_ sender: Any) {
    bgView.backgroundColor = UIColor(white: 1, alpha: 0.9)//change view to bgView[![enter image description here][1]][1]
    popView.isHidden = false
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissView))
    bgView.addGestureRecognizer(tap)//change view to bgView
}

@objc func dismissView() {
    self.popView.isHidden = true
    bgView.backgroundColor = .white//change view to bgView
}

@IBAction func sendButton(_ sender: Any) {
    self.textLabel.text = inputField.text
}