Skip to content

Instantly share code, notes, and snippets.

@gyubokbaik
Created April 19, 2017 22:45
Show Gist options
  • Save gyubokbaik/8bf36effa5264faab1fff681b68e0808 to your computer and use it in GitHub Desktop.
Save gyubokbaik/8bf36effa5264faab1fff681b68e0808 to your computer and use it in GitHub Desktop.
Blog - Hamburger Menu on iOS Done Right - gesturePan Selector
@IBAction func gesturePan(_ sender: UIPanGestureRecognizer) {
// retrieve the current state of the gesture
if sender.state == UIGestureRecognizerState.began {
// no need to do anything
} else if sender.state == UIGestureRecognizerState.changed {
// retrieve the amount viewMenu has been dragged
let translationX = sender.translation(in: sender.view).x
if translationX > 0 {
// viewMenu fully dragged out
constraintMenuLeft.constant = 0
viewBlack.alpha = maxBlackViewAlpha
} else if translationX < -constraintMenuWidth.constant {
// viewMenu fully dragged in
constraintMenuLeft.constant = -constraintMenuWidth.constant
viewBlack.alpha = 0
} else {
// it's being dragged somewhere between min and max amount
constraintMenuLeft.constant = translationX
let ratio = (constraintMenuWidth.constant + translationX) / constraintMenuWidth.constant
let alphaValue = ratio * maxBlackViewAlpha
viewBlack.alpha = alphaValue
}
} else {
// if the drag was 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