IOS仿今日头条滑动导航栏

时间:2022-06-28 06:57:46

之前在服务器之家平台给大家分享了网易首页导航封装类、网易首页导航封装类优化,今天在前两个的基础上仿下今日头条。

1.网易首页导航封装类中主要解决了上面导航的scrollview和下面的页面的scrollview联动的问题,以及上面导航栏的便宜量。

2.网易首页导航封装类优化中主要解决ios7以上滑动返回功能中uiscreenedgepangesturerecognizer与scrollview的滑动的手势冲突问题。

今天仿今日头条滑动导航和网易首页导航封装类优化相似,这个也是解决手势冲突,uipangesturerecognizer与scrollview的手势冲突。

IOS仿今日头条滑动导航栏

一、viewcontroller的层次

用上面的图来介绍,左侧的个人页面viewcontroller上面通过addchildviewcontroller添加了一个以mainviewcontroller为rootviewcontroller的

uinavigationcontroller,通过addsubview将uinavigationcontroller的view添加到个人页面viewcontroller的view上。

二、仿今日头条滑动导航主要有3个问题:

1.uipangesturerecognizer与scrollview的手势冲突

为了达到拖动滑动的效果,需要给mainviewcontroller添加一个uipangesturerecognizer,由于底部是scrollview和tableview组成的,所以会使uipangesturerecognizer与底部的冲突,和网易首页导航封装类优化类似需要在

- (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer中根据gesturerecognizer返回yes来使scrolview和uipangesturerecognizer都识别。

2.uipangesturerecognizer滑动结束的位置不正确

用uipangesturerecognizer滑动根据便宜量来改变uinavigationcontroller的view的位置,在今日头条中滑动结束时uinavigationcontroller的view要么在原位要么在右侧,不会停在中间,这个问题让我想起了之前做的果冻效果,手势有状态state,根据手势的状态来改变uinavigationcontroller的view的位置,特别是在手势结束时。

3.由于1使scrolview和uipangesturerecognizer都识别,导致返回时scrolview也会滑动

让底部的scrolview能滑动的时间应该是uinavigationcontroller的view在初始位置frame的x为0,所以在它的frame的x>0时,底部的bottomscrollview的scrollenabled=no。

三、 主要实现代码(导航的代码就不贴了,只贴主要的)

1.声明一个拖动手势

@property(nonatomic,strong) uipangesturerecognizer *pangesturerecognizer;

2.为mainviewcontroller添加手势

?
1
2
3
_pangesturerecognizer=[[uipangesturerecognizer alloc]initwithtarget:self action:@selector(pangesture:)];
_pangesturerecognizer.delegate=self;
[self.navigationcontroller.view addgesturerecognizer:_pangesturerecognizer];

3.手势识别的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//平移
-(void)pangesture:(uipangesturerecognizer*)pan
{
//在view中的位置
// cgpoint point=[pan locationinview:self.navigationcontroller.view];
//在view中的移动量 以手指按下的为原点
cgpoint point1=[pan translationinview:self.navigationcontroller.view];
if (pan.state==uigesturerecognizerstatechanged&&pan==_pangesturerecognizer) {
if (point1.x>0&&self.navigationcontroller.view.frame.origin.x<self.navigationcontroller.view.frame.size.width-100) {
float x= self.navigationcontroller.view.frame.origin.x+point1.x>self.navigationcontroller.view.frame.size.width-100?self.navigationcontroller.view.frame.size.width-100:self.navigationcontroller.view.frame.origin.x+point1.x;
self.navigationcontroller.view.frame=cgrectmake(x, self.navigationcontroller.view.frame.origin.y, self.navigationcontroller.view.frame.size.width, self.navigationcontroller.view.frame.size.height);
nslog(@"%@",nsstringfromcgrect(self.navigationcontroller.view.frame));
}
else if(point1.x<0)
{
nslog(@"aaa %f",self.navigationcontroller.view.frame.origin.x);
float x=self.navigationcontroller.view.frame.origin.x+point1.x<0?0:self.navigationcontroller.view.frame.origin.x+point1.x;
self.navigationcontroller.view.frame=cgrectmake(x, self.navigationcontroller.view.frame.origin.y, self.navigationcontroller.view.frame.size.width, self.navigationcontroller.view.frame.size.height);
}
}
else if(pan.state==uigesturerecognizerstateended)
{
if (self.navigationcontroller.view.frame.origin.x>self.navigationcontroller.view.frame.size.width/3) {
[uiview animatewithduration:0.2 animations:^{
self.navigationcontroller.view.frame=cgrectmake(self.navigationcontroller.view.frame.size.width-100, 0, self.navigationcontroller.view.frame.size.width, self.navigationcontroller.view.frame.size.height);
} completion:^(bool finished) {
self.bottomscrollview.scrollenabled=yes;
}];
}
else
{
[uiview animatewithduration:0.2 animations:^{
self.navigationcontroller.view.frame=cgrectmake(0, 0, self.navigationcontroller.view.frame.size.width, self.navigationcontroller.view.frame.size.height);
} completion:^(bool finished) {
self.bottomscrollview.scrollenabled=yes;
}];
}
}
//偏移量是增加的应该设为0
[pan settranslation:cgpointzero inview:self.navigationcontroller.view];
}

4.手势冲突解决

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (bool)gesturerecognizer:(uigesturerecognizer *)gesturerecognizer shouldrecognizesimultaneouslywithgesturerecognizer:(uigesturerecognizer *)othergesturerecognizer
{
if (self.bottomscrollview.contentoffset.x<=0.0&&gesturerecognizer==_pangesturerecognizer) {
if (self.navigationcontroller.view.frame.origin.x>0) {
self.bottomscrollview.scrollenabled=no;
}
else
{
self.bottomscrollview.scrollenabled=yes;
}
return yes;
}
return no;
}

四、效果图

IOS仿今日头条滑动导航栏IOS仿今日头条滑动导航栏

以上所述是小编给大家分享的ios仿今日头条滑动导航栏,希望对大家有所帮助。