按下视图时如何 hide/unhide 按钮?

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

我的 cameraView 上有这些按钮,我想在按下视图时隐藏这些按钮。我让它工作,但我想在再次按下视图时取消隐藏按钮。我怎样才能做到这一点?

    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")

    }
    

如果你想在每次触摸时将 isHidden 值更改为相反的值,你可以简单地使用 toggle() 函数,它切换 Bool 值 - 它分配 false 如果值为 true 并分配 true 如果值为 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()
    }
}