Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active November 28, 2017 10:06
Show Gist options
  • Save brennanMKE/38fea29e8ea8893300cc to your computer and use it in GitHub Desktop.
Save brennanMKE/38fea29e8ea8893300cc to your computer and use it in GitHub Desktop.
FullScreen interaction for setting the top bars to be clear until content is scrolled up and the background has to be opaque.
#pragma mark - Hide and Show
#pragma mark -
- (void)prepareAnimationForNavigationBarWithDuration:(CGFloat)duration {
// prepare animation for navigation bar
CATransition *animation = [CATransition animation];
[animation setDuration:duration];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setType:kCATransitionFade];
[self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
}
- (void)hideTopBars:(BOOL)animated withCompletionBlock:(void (^)())completionBlock {
// sets a clear background for the top bars
_topBarsClear = TRUE;
CGFloat duration = animated ? 0.25f : 0.0f;
[self prepareAnimationForNavigationBarWithDuration:duration];
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
} completion:^(BOOL finished) {
if (finished && completionBlock) {
completionBlock();
}
}];
}
- (void)showTopBars:(BOOL)animated withCompletionBlock:(void (^)())completionBlock {
// sets the top bars to show an opaque background
_topBarsClear = FALSE;
CGFloat duration = animated ? 0.25f : 0.0f;
[self prepareAnimationForNavigationBarWithDuration:duration];
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:duration delay:0.0 options:options animations:^{
UIImage *backgroundImage = [SSTStyleKit imageOfBlueTopBarBackgroundImage];
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
} completion:^(BOOL finished) {
if (finished && completionBlock) {
completionBlock();
}
}];
}
#pragma mark - UIScrollViewDelegate
#pragma mark -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//NSLog(@"offset: %f", scrollView.contentOffset.y);
if (_topBarsClear) {
if (scrollView.contentOffset.y > kCutOffPoint) {
[self showTopBars:TRUE withCompletionBlock:nil];
}
}
else {
if (scrollView.contentOffset.y <= kCutOffPoint) {
[self hideTopBars:TRUE withCompletionBlock:nil];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment