当前位置:主页 > 移动开发 > iOS代码 >

iOS实现scrollview上拉显示Navbar下拉隐藏功能详解

时间:2020-10-21 20:59:35 | 栏目:iOS代码 | 点击:

本文主要介绍的是关于iOS中scrollview上拉显示Navbar下拉隐藏的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:

动画效果:


关于下拉隐藏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];
}

总结

您可能感兴趣的文章:

相关文章