Search code examples
swiftuitapgesturerecognizertouchesbegan

How would I hide/unhide buttons when pressing on the view?


I have these buttons on my cameraView that I want to hide when pressing on the view. I got that to work but I want to unhide the buttons when I press on the view again. How would I be able to do that?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    
    if touch?.view == self.cameraView {
      
        flipCamera.isHidden = true
        lockButton.isHidden = true
        print("Hide buttons")
    
    } else if touch?.view == self.cameraView && flipCamera.isHidden == true {
        

        print("show buttons")

    }
    

Solution

  • If you want to change the isHidden value to its opposite on each touch, you can simply use the toggle() function, which toggles a Bool value - it assigns false if the value was true and assigns true if the value was false.

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        
        if touch?.view == self.cameraView {
            flipCamera.isHidden.toggle()
            lockButton.isHidden.toggle()
        }
    }