Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

431
Views
Cómo descartar UIView al tocar Swift4

Tengo una UIView que se muestra cada vez que presiono un botón en otra vista

 @IBOutlet weak var view1: UIView! @IBOutlet weak var view2: UIView! @IBAction func showView(_ sender: Any) { view2.isHidden = false }

Lo que quiero es agregar un gesto de toque que me permita ocultar view2 cada vez que toco fuera de la vista y, dado que esas vistas se pueden arrastrar, quiero que la segunda vista no se pueda tocar cuando esté oculta (de modo que si toco debajo de mi view No me arriesgo a moverlo Esto es lo que probé:

  1.  var gesture : UITapGestureRecognizer? override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(closeView), name: NSNotification.Name("CloseView"), object: nil) gesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.closeView)) } @objc func closeView() { if view2.isHidden == false { view2.isUserInteractionEnabled = true view2.isHidden = false self.view.removeGestureRecognizer(gesture!) } else { view2.isHidden = true view2.isUserInteractionEnabled = true self.view.addGestureRecognizer(gesture!) } }
  2.  let closeTapGesture = UITapGestureRecognizer(target: view, action: #selector(getter: view2.isHidden) view.addGestureRecognizer(closeTapGesture)

Nada de este trabajo, ¿cómo puedo hacer?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe verificar si realmente tocó fuera de view2:

 var gesture : UITapGestureRecognizer? override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(closeView), name: NSNotification.Name("CloseView"), object: nil) let gesture = UITapGestureRecognizer(target: self, action: #selector(closeView(_:))) view.addGestureRecognizer(gesture) self.gesture = gesture } @objc private func closeView(_ tapGestureRecognizer: UITapGestureRecognizer) { let location = tapGestureRecognizer.location(in: view2) guard view2.isHidden == false, !view2.bounds.contains(location) else { //We need to have tapped outside of view 2 return } view2.isHidden = true }
over 4 years ago · Santiago Trujillo Report

0

Su gesto de toque solo debe manejar closeView .

 @objc func closeView() { view2.isHidden = true view2.isUserInteractionEnabled = false gesture?.isEnabled = false }

Y el clic del botón para mostrar su vista2 debería llamar a esto.

 func showView() { view2.isHidden = false view2.isUserInteractionEnabled = true gesture?.isEnabled = true }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!