动画效果:
关于下拉隐藏Nabbar,上拉显示,有三种方式:推崇第一种,但是做的还是没简书、知乎那样流畅,第一种是对navbar做了平移,第二种,第三种都是隐藏。。
第一种,和第二种处理时机一样,但是效果更好,并没有处理Navbar 的隐藏。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
oldY = scrollView.contentOffset.y;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (scrollView.contentOffset.y > oldY) {
[UIView animateWithDuration:0.3 animations:^{
self.navigationController.navigationBar.transform =
CGAffineTransformMakeTranslation(0, -64);
}];
;
}else{
[UIView animateWithDuration:0.3 animations:^{
self.navigationController.navigationBar.transform =
CGAffineTransformIdentity;
}];
}
}
-(void)viewWillDisappear:(BOOL)animated{
self.navigationController.navigationBar.transform = CGAffineTransformIdentity;
}
第二种:在开始拖拽的时候保存偏移量,结束拖拽的时候比较当前偏移量,判断是否隐藏Navbar。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
oldY = scrollView.contentOffset.y;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (scrollView.contentOffset.y > oldY) {
[CATransaction begin];
[CATransaction setAnimationDuration:1.0];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
//set the position
[self.navigationController setNavigationBarHidden:YES animated:YES];
[CATransaction commit];
;
}else{
[CATransaction begin];
[CATransaction setAnimationDuration:1.0];
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[CATransaction commit];
}
}
第三种: 是根据拖拽的velocity进行判断,很霸道但是效果不如第一种,,隐藏的时机不对你可以做个比较;
这种方法引自文章:滑动隐藏
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if(velocity.y>0)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
最后选择合适的时候恢复Navbar 的状态,建议放在viewWillDisappear里,否则在进入下一个页面里很突兀。
-(void)viewWillDisappear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}