iosswiftxcodeuiviewanimatewithduration

Prevent click through view in swift


I'm working in Xcode and swift, I created a view acting as a menu that toggles on tap, when the menu comes out I can still click a test button underneath it. I don't want that to happen. I want everything behind the view to be disabled giving priority to the menu view. (View Image below)

screenshot from the sample app

this is the code that I'm using:

@IBAction func MenuButton(sender: UIButton) {
    if self.MenuView.frame.origin.x == -180 {
        UIView.animateWithDuration(0.5, animations:{
            self.MenuView.frame = CGRectMake(self.MenuView.frame.origin.x + 180, self.MenuView.frame.origin.y, self.MenuView.frame.size.width, self.MenuView.frame.size.height)
        })

    } else {
        UIView.animateWithDuration(0.5, animations:{
            self.MenuView.frame = CGRectMake(self.MenuView.frame.origin.x - 180, self.MenuView.frame.origin.y, self.MenuView.frame.size.width, self.MenuView.frame.size.height)
        })
    }
}

the view is hidden 180 pixels on the left side, when the menu button is clicked the view will animate 180 pixels to the right which brings it to the front. The function checks if the view is already opened so it can animate it back 180 pixel to hide it.

The only thing I need is to disable clicking through the view.


Solution

  • The fact that the button is still visible and clickable means that it must be in front of the menu. If you rearrange the order of things so that the menu is in front of the button, then you should get the result that you are looking for.