Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

427
Vistas
How to dismiss UIView on tap Swift4

I have a UIView that is showed every time I press a button in another view

@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView! 

@IBAction func showView(_ sender: Any) {
    view2.isHidden = false
}

What I want is to add a tap gesture that allows me to hide view2 every time I tap outside of the view and, since those views are draggable, I want the second view not to be tappable when hidden ( so that if I touch under my view I don't risk to move it. This is what I tried:

  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)
    

None of this work, how can I do?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You need to check if you actually tapped outside of 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 Denunciar

0

Your tap gesture should only handle closeView .

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

And the the button click to show your view2 should call this.

func  showView() {
    view2.isHidden = false
    view2.isUserInteractionEnabled = true
    gesture?.isEnabled = true
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda