Skip to content

Instantly share code, notes, and snippets.

@jamesrochabrun
Created November 9, 2019 23:10
Show Gist options
  • Save jamesrochabrun/a37043a361215e94964ffc39d6757dc5 to your computer and use it in GitHub Desktop.
Save jamesrochabrun/a37043a361215e94964ffc39d6757dc5 to your computer and use it in GitHub Desktop.
Math to animate Navigation bar on Scrolling
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let safeAreaTop = UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.safeAreaInsets.top ?? 0
let navigationNeededOfset: CGFloat = safeAreaTop// + (navigationController?.navigationBar.frame.height ?? 0)
let offset = scrollView.contentOffset.y + navigationNeededOfset
let alpha = 1 - ((scrollView.contentOffset.y + navigationNeededOfset) / navigationNeededOfset)
// Use the alpha here
navigationController?.navigationBar.backgroundColor = .blue
navigationController?.navigationBar.transform = .init(translationX: 0, y: min(0, -offset))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment