IOS仿今日頭條滑動(dòng)導(dǎo)航欄
之前在腳本之家平臺(tái)給大家分享了網(wǎng)易首頁(yè)導(dǎo)航封裝類、網(wǎng)易首頁(yè)導(dǎo)航封裝類優(yōu)化,今天在前兩個(gè)的基礎(chǔ)上仿下今日頭條。
1.網(wǎng)易首頁(yè)導(dǎo)航封裝類中主要解決了上面導(dǎo)航的ScrollView和下面的頁(yè)面的ScrollView聯(lián)動(dòng)的問(wèn)題,以及上面導(dǎo)航欄的便宜量。
2.網(wǎng)易首頁(yè)導(dǎo)航封裝類優(yōu)化中主要解決iOS7以上滑動(dòng)返回功能中UIScreenEdgePanGestureRecognizer與ScrollView的滑動(dòng)的手勢(shì)沖突問(wèn)題。
今天仿今日頭條滑動(dòng)導(dǎo)航和網(wǎng)易首頁(yè)導(dǎo)航封裝類優(yōu)化相似,這個(gè)也是解決手勢(shì)沖突,UIPanGestureRecognizer與ScrollView的手勢(shì)沖突。

一、ViewController的層次
用上面的圖來(lái)介紹,左側(cè)的個(gè)人頁(yè)面ViewController上面通過(guò)addChildViewController添加了一個(gè)以MainViewController為RootViewController的
UINavigationController,通過(guò)addSubview將UINavigationController的View添加到個(gè)人頁(yè)面ViewController的View上。
二、仿今日頭條滑動(dòng)導(dǎo)航主要有3個(gè)問(wèn)題:
1.UIPanGestureRecognizer與ScrollView的手勢(shì)沖突
為了達(dá)到拖動(dòng)滑動(dòng)的效果,需要給MainViewController添加一個(gè)UIPanGestureRecognizer,由于底部是ScrollView和TableView組成的,所以會(huì)使UIPanGestureRecognizer與底部的沖突,和網(wǎng)易首頁(yè)導(dǎo)航封裝類優(yōu)化類似需要在
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer中根據(jù)gestureRecognizer返回YES來(lái)使ScrolView和UIPanGestureRecognizer都識(shí)別。
2.UIPanGestureRecognizer滑動(dòng)結(jié)束的位置不正確
用UIPanGestureRecognizer滑動(dòng)根據(jù)便宜量來(lái)改變UINavigationController的View的位置,在今日頭條中滑動(dòng)結(jié)束時(shí)UINavigationController的View要么在原位要么在右側(cè),不會(huì)停在中間,這個(gè)問(wèn)題讓我想起了之前做的果凍效果,手勢(shì)有狀態(tài)state,根據(jù)手勢(shì)的狀態(tài)來(lái)改變UINavigationController的View的位置,特別是在手勢(shì)結(jié)束時(shí)。
3.由于1使ScrolView和UIPanGestureRecognizer都識(shí)別,導(dǎo)致返回時(shí)ScrolView也會(huì)滑動(dòng)
讓底部的ScrolView能滑動(dòng)的時(shí)間應(yīng)該是UINavigationController的View在初始位置frame的x為0,所以在它的frame的x>0時(shí),底部的bottomScrollView的scrollEnabled=NO。
三、 主要實(shí)現(xiàn)代碼(導(dǎo)航的代碼就不貼了,只貼主要的)
1.聲明一個(gè)拖動(dòng)手勢(shì)
@property(nonatomic,strong) UIPanGestureRecognizer *panGestureRecognizer;
2.為MainViewController添加手勢(shì)
_panGestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)]; _panGestureRecognizer.delegate=self; [self.navigationController.view addGestureRecognizer:_panGestureRecognizer];
3.手勢(shì)識(shí)別的方法
//平移
-(void)panGesture:(UIPanGestureRecognizer*)pan
{
//在View中的位置
// CGPoint point=[pan locationInView:self.navigationController.view];
//在View中的移動(dòng)量 以手指按下的為原點(diǎn)
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;
}];
}
}
//偏移量是增加的應(yīng)該設(shè)為0
[pan setTranslation:CGPointZero inView:self.navigationController.view];
}
4.手勢(shì)沖突解決
- (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仿今日頭條滑動(dòng)導(dǎo)航欄,希望對(duì)大家有所幫助。
- ios scrollview嵌套tableview同向滑動(dòng)的示例
- iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
- iOS滑動(dòng)解鎖、滑動(dòng)獲取驗(yàn)證碼效果的實(shí)現(xiàn)代碼
- 詳解iOS中position:fixed吸底時(shí)的滑動(dòng)出現(xiàn)抖動(dòng)的解決方案
- 微信小程序在ios下Echarts圖表不能滑動(dòng)的問(wèn)題解決
- 微信瀏覽器彈出框滑動(dòng)時(shí)頁(yè)面跟著滑動(dòng)的實(shí)現(xiàn)代碼(兼容Android和IOS端)
- iOS 頁(yè)面滑動(dòng)與標(biāo)題切換顏色漸變的聯(lián)動(dòng)效果實(shí)例
- IOS開發(fā)中禁止NavigationController的向右滑動(dòng)返回
- iOS開發(fā)上下滑動(dòng)UIScrollview隱藏或者顯示導(dǎo)航欄的實(shí)例
- iOS實(shí)現(xiàn)無(wú)限滑動(dòng)效果
相關(guān)文章
iOS開發(fā)中以application/json上傳文件實(shí)例詳解
在和sever后臺(tái)交互的過(guò)程中、有時(shí)候、他們需要我們iOS開發(fā)者以“application/json”形式上傳,具體實(shí)例代碼大家參考下本文2017-07-07
詳解iOS開發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問(wèn)題
這篇文章主要介紹了詳解iOS開發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
iOS App開發(fā)中通過(guò)UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來(lái)監(jiān)測(cè)iOS設(shè)備的電量了,然后再實(shí)現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來(lái)總結(jié)一下iOS App開發(fā)中通過(guò)UIDevice類獲取設(shè)備信息的方法:2016-07-07
解析iOS應(yīng)用的UI開發(fā)中懶加載和xib的簡(jiǎn)單使用方法
這篇文章主要介紹了解析iOS應(yīng)用的UI開發(fā)中懶加載和xib的簡(jiǎn)單使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
iOS使用WKWebView加載HTML5不顯示屏幕寬度的問(wèn)題解決
這篇文章主要介紹了iOS使用WKWebView加載HTML5不顯示屏幕寬度的問(wèn)題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
iOS中長(zhǎng)條藍(lán)色按鈕(button)實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家介紹了iOS中長(zhǎng)條藍(lán)色按鈕(button)實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-08-08

