Skip to content

Instantly share code, notes, and snippets.

@Lucien
Last active August 25, 2021 18:08
Show Gist options
  • Save Lucien/7217914 to your computer and use it in GitHub Desktop.
Save Lucien/7217914 to your computer and use it in GitHub Desktop.
Moving navBar with scrollViewDidScroll:
typedef NS_ENUM(NSInteger, ScrollDirection)
{
ScrollDirectionDown = -1,
ScrollDirectionNone,
ScrollDirectionUp
};
###################################
[RPMDefines statusBarSize].height = 20;
_defaultNavHeight = 44. + 20.;
###################################
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
RPMDbgLog(@"navBar originY: %f", _navBar.frame.origin.y);
ScrollDirection direction = (scrollView.contentOffset.y > _lastContentOffsetY) ? ScrollDirectionDown : ScrollDirectionUp;
if (scrollView.contentOffset.y == _lastContentOffsetY)
direction = ScrollDirectionNone;
_scrollDirection = direction;
_lastContentOffsetY = scrollView.contentOffset.y;
// navBar adjustments on scroll
CGFloat originY = _navBar.frame.origin.y;
CGRect navBarFrame = _navBar.frame;
CGFloat alpha = (ABS([RPMDefines statusBarSize].height - _defaultNavHeight) + originY) / ABS([RPMDefines statusBarSize].height - _defaultNavHeight);
_navItem.titleView.alpha = alpha;
if ((scrollView.contentOffset.y > -_defaultNavHeight))
{
if (_scrollDirection == ScrollDirectionUp)
{
originY++;
if (originY > 0)
{
originY = 0;
}
}
else if (_scrollDirection == ScrollDirectionDown)
{
originY--;
if (originY < [RPMDefines statusBarSize].height - _defaultNavHeight)
{
originY = [RPMDefines statusBarSize].height - _defaultNavHeight;
}
}
navBarFrame.origin.y = originY;
_navBar.frame = navBarFrame;
}
else
{
navBarFrame.origin.y = 0;
_navBar.frame = navBarFrame;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment