NJKScrollFullSreen 是一个类似 Facebook App 的全屏滚动库。
此仓库包含全屏代理和全屏 UI 行为模块。
NJKScrollFullSreen
UIViewController+NJKFullScreenSupport
这些模块是独立的。您可以在不使用 UIViewController+NJKFullScreenSupport
的情况下实现自己的自定义全屏行为。NJKScrollFullSreen
不仅适用于 UIScrollView,还适用于 UIWebView 和 UITableView。
NJKScrollFullScreen
在您的视图控制器上实例化并设置 UIScrollViewDelegate
。如果您设置了 scrollViewDelegate
,则 NJKScrollFullScreen
应作为代理对象执行。
- (void)viewDidLoad
{
[super viewDidLoad];
_scrollProxy = [[NJKScrollFullScreen alloc] initWithForwardTarget:self]; // UIScrollViewDelegate and UITableViewDelegate methods proxy to ViewController
self.tableView.delegate = (id)_scrollProxy; // cast for surpress incompatible warnings
_scrollProxy.delegate = self;
}
- (void)scrollFullScreen:(NJKScrollFullScreen *)proxy scrollViewDidScrollUp:(CGFloat)deltaY
{
[self moveNavigationBar:deltaY animated:YES];
}
- (void)scrollFullScreen:(NJKScrollFullScreen *)proxy scrollViewDidScrollDown:(CGFloat)deltaY
{
[self moveNavigationBar:deltaY animated:YES];
}
- (void)scrollFullScreenScrollViewDidEndDraggingScrollUp:(NJKScrollFullScreen *)proxy
{
[self hideNavigationBar:YES];
}
- (void)scrollFullScreenScrollViewDidEndDraggingScrollDown:(NJKScrollFullScreen *)proxy
{
[self showNavigationBar:YES];
}
您可以选择 UIViewController+NJKFullScreenSupport
或您自己的视图管理代码。
使用 UIViewController+NJKFullScreenSupport.h
。
#import "UIViewController+NJKFullScreenSupport.h"
或者,您可以像下面一样实现自己的全屏行为。
- (void)showNavigationBar:(BOOL)animated
{
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
[self setNavigationBarOriginY:statusBarHeight animated:animated];
}
- (void)hideNavigationBar:(BOOL)animated
{
[self setNavigationBarOriginY:0 animated:animated];
}
- (void)moveNavigationBar:(CGFloat)deltaY animated:(BOOL)animated
{
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat nextY = frame.origin.y + deltaY;
[self setNavigationBarOriginY:nextY animated:animated];
}
- (void)setNavigationBarOriginY:(CGFloat)y animated:(BOOL)animated
{
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat navigationBarHeight = frame.size.height;
frame.origin.y = fmin(fmax(y, navigationBarHeight), statusBarHeight); // limit over moving
[UIView animateWithDuration:animated ? 0.1 : 0 animations:^{
self.navigationController.navigationBar.frame = frame;
}];
}