Last active
November 30, 2022 03:33
-
-
Save ccabanero/7c47bcb06573a17f9858 to your computer and use it in GitHub Desktop.
Fade Out TitleView on Swipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
For configuring the NavigationBar to show/hide when user swipes | |
- returns: void | |
*/ | |
func configureNavigationBarAsHideable() { | |
if let navigationController = self.navigationController { | |
// respond to swipe and hide/show | |
navigationController.hidesBarsOnSwipe = true | |
// get the pan gesture used to trigger hiding/showing the NavigationBar | |
let panGestureRecognizer = navigationController.barHideOnSwipeGestureRecognizer | |
// when the user pans, call action method to fade out title view | |
panGestureRecognizer.addTarget(self, action:"fadeOutTitleView:") | |
} | |
} | |
/** | |
For fading out the title view | |
- parameter sender: The UIPanGestureRecognizer when user swipes (and hides/shows NavigationBar)) | |
- returns: void | |
*/ | |
func fadeOutTitleView(sender: UIPanGestureRecognizer) { | |
if let titleView = self.navigationItem.titleView { | |
// fade out title view when swiping up | |
let translation = sender.translationInView(self.view) | |
if(translation.y < 0) { | |
let alphaValue = 1 - abs(translation.y / titleView.frame.height) | |
titleView.alpha = alphaValue | |
} | |
// clear transparency if the Navigation Bar is not hidden after swiping | |
if let navigationController = self.navigationController { | |
let navigationBar = navigationController.navigationBar | |
if navigationBar.frame.origin.y > 0 { | |
if let titleView = self.navigationItem.titleView { | |
titleView.alpha = 1 | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment