效果图
代码实现
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView* scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"首页";
[self.view addSubview:self.scrollView];
self.scrollView.contentSize = CGSizeMake(0, 3*self.view.bounds.size.height);
}
//当scrollView滚动的时候调用的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
UIPanGestureRecognizer* pan = scrollView.panGestureRecognizer;
//获取到拖拽的速度 >0 向下拖动 <0 向上拖动
CGFloat velocity = [pan velocityInView:scrollView].y;
if (velocity<-5) {
//向上拖动,隐藏导航栏
[self.navigationController setNavigationBarHidden:true animated:true];
}
else if (velocity>5) {
//向下拖动,显示导航栏
[self.navigationController setNavigationBarHidden:false animated:true];
}
else if(velocity==0){
//停止拖拽
}
}
//view将要消失的时候,显示导航栏,这样跳转到其他界面的时候,才能看到导航栏,否则,看不到导航栏
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:false animated:true];
}
- (UIScrollView *)scrollView{
if (_scrollView==nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
_scrollView.backgroundColor = [UIColor orangeColor];
_scrollView.delegate = self;
}
return _scrollView;
}