Skip to content

Instantly share code, notes, and snippets.

@gyubokbaik
Created April 19, 2017 22:29
Show Gist options
  • Save gyubokbaik/65e8d4efc6157021e904711a5f4a307f to your computer and use it in GitHub Desktop.
Save gyubokbaik/65e8d4efc6157021e904711a5f4a307f to your computer and use it in GitHub Desktop.
Blog - Hamburger Menu on iOS Done Right - gestureScreenEdgePan Selector
@IBAction func gestureScreenEdgePan(_ sender: UIScreenEdgePanGestureRecognizer) {
// retrieve the current state of the gesture
if sender.state == UIGestureRecognizerState.began {
// if the user has just started dragging, make sure view for dimming effect is hidden well
viewBlack.isHidden = false
viewBlack.alpha = 0
} else if (sender.state == UIGestureRecognizerState.changed) {
// retrieve the amount viewMenu has been dragged
let translationX = sender.translation(in: sender.view).x
if -constraintMenuWidth.constant + translationX > 0 {
// viewMenu fully dragged out
constraintMenuLeft.constant = 0
viewBlack.alpha = maxBlackViewAlpha
} else if translationX < 0 {
// viewMenu fully dragged in
constraintMenuLeft.constant = -constraintMenuWidth.constant
viewBlack.alpha = 0
} else {
// viewMenu is being dragged somewhere between min and max amount
constraintMenuLeft.constant = -constraintMenuWidth.constant + translationX
let ratio = translationX / constraintMenuWidth.constant
let alphaValue = ratio * maxBlackViewAlpha
viewBlack.alpha = alphaValue
}
} else {
// if the menu was dragged less than half of it's width, close it. Otherwise, open it.
if constraintMenuLeft.constant < -constraintMenuWidth.constant / 2 {
self.hideMenu()
} else {
self.openMenu()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment