iOS开发上下滑动UIScrollview隐藏或者显示导航栏

2017-12-01  本文已影响13人  木子木木易

好多App都有上下滑动UIScrollview隐藏或者显示导航栏,在这里我说说我觉得有用的几种方法:

1.iOS8之后系统有一个属性hidesBarsOnSwipe

Objective-C代码如下

self.navigationController.hidesBarsOnSwipe=YES;

swift代码如下

self.navigationController?.hidesBarsOnSwipe=true

当使用以上代码时,可以达到效果

2.使用UIScrollViewDelegate一个代理方法

Objective-C代码如下

- (void)scrollViewDidScroll:(UIScrollView*)scrollView

{

//scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势

UIPanGestureRecognizer*pan = scrollView.panGestureRecognizer;

//获取到拖拽的速度 >0 向下拖动 <0 向上拖动

CGFloat velocity = [panvelocityInView:scrollView].y;

if(velocity <-5) {

//向上拖动,隐藏导航栏

[self.navigationControllersetNavigationBarHidden:YESanimated:YES];

}elseif(velocity >5) {

//向下拖动,显示导航栏

[self.navigationControllersetNavigationBarHidden:NOanimated:YES];

}elseif(velocity ==0){

//停止拖拽

}

}

swift代码如下

func scrollViewDidScroll(scrollView: UIScrollView) {

let pan = scrollView.panGestureRecognizer

let velocity = pan.velocityInView(scrollView).y

ifvelocity < -5{

self.navigationController?.setNavigationBarHidden(true,animated:true)

}elseifvelocity >5{

self.navigationController?.setNavigationBarHidden(false,animated:true)

}

}

这种效果最好

3.使用UIScrollViewDelegate另一个代理方法

Objective-C代码如下

- (void)scrollViewWillEndDragging:(UIScrollView*)scrollViewwithVelocity:(CGPoint)velocitytargetContentOffset:(inoutCGPoint*)targetContentOffset

{

if(velocity.y>0.0) {

[self.navigationControllersetNavigationBarHidden:YESanimated:YES];

}elseif(velocity.y<0.0){

[self.navigationControllersetNavigationBarHidden:NOanimated:YES];

}

}

swift代码如下

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocityvelocity: CGPoint,targetContentOffset: UnsafeMutablePointer) {

ifvelocity.y>0{

self.navigationController?.setNavigationBarHidden(true,animated:true)

}elseifvelocity.y<0{

self.navigationController?.setNavigationBarHidden(false,animated:true)

}

}


以上内容转载:http://blog.csdn.net/wgl_happy/article/details/51791937

上一篇下一篇

猜你喜欢

热点阅读