Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gyubokbaik/d45d2e398ccdb258617172609608cfd0 to your computer and use it in GitHub Desktop.
Save gyubokbaik/d45d2e398ccdb258617172609608cfd0 to your computer and use it in GitHub Desktop.
Blog - Hamburger Menu on iOS Done Right - Open and Close Menu Convenience Functions
func openMenu() {
// when menu is opened, it's left constraint should be 0
constraintMenuLeft.constant = 0
// view for dimming effect should also be shown
viewBlack.isHidden = false
// animate opening of the menu - including opacity value
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
self.viewBlack.alpha = self.maxBlackViewAlpha
}, completion: { (complete) in
// disable the screen edge pan gesture when menu is fully opened
self.gestureScreenEdgePan.isEnabled = false
})
}
func hideMenu() {
// when menu is closed, it's left constraint should be of value that allows it to be completely hidden to the left of the screen - which is negative value of it's width
constraintMenuLeft.constant = -constraintMenuWidth.constant
// animate closing of the menu - including opacity value
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
self.viewBlack.alpha = 0
}, completion: { (complete) in
// reenable the screen edge pan gesture so we can detect it next time
self.gestureScreenEdgePan.isEnabled = true
// hide the view for dimming effect so it wont interrupt touches for views underneath it
self.viewBlack.isHidden = true
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment