




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在iOS中實(shí)現(xiàn)一個(gè)透明導(dǎo)航欄
這篇文章給大家介紹怎么在iOS中實(shí)現(xiàn)一個(gè)透明導(dǎo)航欄,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。首先在需要設(shè)置導(dǎo)航欄透明的頁(yè)面的viewDidload中寫(xiě)上self.title
=
@"Title";
[self.navigationController.navigationBar
setBackgroundImage:[UIImage
new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage
=
[UIImage
new];
self.barImageView
=
self.navigationController.navigationBar.subviews.firstObject;
self.barImageView.alpha
=
0;
//設(shè)置狀態(tài)欄
[[UIApplication
sharedApplication]
setStatusBarStyle:UIStatusBarStyleLightContent];
//設(shè)置標(biāo)題顏色
self.navigationController.navigationBar.titleTextAttributes
=
@{NSForegroundColorAttributeName
:
[UIColor
clearColor]};在scrollViewDidScroll代理方法中-(void)scrollViewDidScroll:(UIScrollView
*)scrollView
{
CGFloat
offset
=
scrollView.contentOffset.y;
//根據(jù)自己需要設(shè)置(136)的大小
CGFloat
alpha
=
offset
/
136;
_barImageView.alpha
=
alpha;
//記錄下當(dāng)前的透明度,在返回當(dāng)前頁(yè)面時(shí)需要
_alpha
=
alpha;
[[NSUserDefaults
standardUserDefaults]
setObject:[NSNumber
numberWithFloat:alpha]
forKey:@"_alpha"];
//設(shè)置標(biāo)題的透明度
self.navigationController.navigationBar.titleTextAttributes
=
@{NSForegroundColorAttributeName
:
[UIColor
colorWithWhite:0
alpha:alpha]};
}當(dāng)前頁(yè)的viewWillAppear,viewDidAppear,viewWillDisappear-(void)viewWillAppear:(BOOL)animated
{
[super
viewWillAppear:animated];
self.table.delegate
=
self;
}
-(void)viewDidAppear:(BOOL)animated
{
BOOL
isGesturePop
=
[[[NSUserDefaults
standardUserDefaults]
objectForKey:@"isGesturePop"]
boolValue];
if
(!isGesturePop)
{
_barImageView.alpha
=
_alpha;
self.navigationController.navigationBar.titleTextAttributes
=
@{NSForegroundColorAttributeName
:
[UIColor
colorWithWhite:0
alpha:_alpha]};
}
[super
viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super
viewWillDisappear:animated];
self.table.delegate
=
nil;
self.navigationController.navigationBar.titleTextAttributes
=
@{NSForegroundColorAttributeName
:
[UIColor
blackColor]};
_barImageView.alpha
=
1;
[[NSUserDefaults
standardUserDefaults]
setObject:[NSNumber
numberWithBool:NO]
forKey:@"isGesturePop"];
}那么在我們需要push的下一個(gè)頁(yè)面需要什么操作呢,我們需要在這個(gè)頁(yè)面顯示正常的nav并且禁掉系統(tǒng)的手勢(shì)pop,自己寫(xiě)一個(gè)pop手勢(shì),以方便我們拿到pop滑動(dòng)時(shí)的偏移量,在做的時(shí)候使用了兩個(gè)類,在最后會(huì)有源碼貼出B.m須遵守UIGestureRecognizerDelegate,并導(dǎo)入NavigationInteractiveTransition.h全局變量@property
(nonatomic,
strong)
NavigationInteractiveTransition
*navT;viewDidLoadself.navigationCeractivePopGestureRecognizer.enabled
=
NO;
UIGestureRecognizer
*gesture
=
self.navigationCeractivePopGestureRecognizer;
gesture.enabled
=
NO;
UIView
*gestureView
=
gesture.view;
UIPanGestureRecognizer
*popRecognizer
=
[[UIPanGestureRecognizer
alloc]
init];
popRecognizer.delegate
=
self;
popRecognizer.maximumNumberOfTouches
=
1;
[gestureView
addGestureRecognizer:popRecognizer];
_navT
=
[[NavigationInteractiveTransition
alloc]
initWithViewController:self.navigationController];
[popRecognizer
addTarget:_navT
action:@selector(handleControllerPop:)];UIGestureRecognizerDelegate代理方法gestureRecognizerShouldBegin-
(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
*)gestureRecognizer
{
//記錄當(dāng)前是是否是通過(guò)手勢(shì)滑動(dòng)回去
[[NSUserDefaults
standardUserDefaults]
setObject:[NSNumber
numberWithBool:YES]
forKey:@"isGesturePop"];
/**
*
這里有兩個(gè)條件不允許手勢(shì)執(zhí)行,1、當(dāng)前控制器為根控制器;2、如果這個(gè)push、pop動(dòng)畫(huà)正在執(zhí)行(私有屬性)
*/
return
self.navigationController.viewControllers.count
!=
1
&&
![[self.navigationController
valueForKey:@"_isTransitioning"]
boolValue];
}需要依賴的兩個(gè)類源碼NavigationInteractiveTransition.h#import
<UIKit/UIKit.h>
@class
UIViewController,
UIPercentDrivenInteractiveTransition;
@interface
NavigationInteractiveTransition
:
NSObject
<UINavigationControllerDelegate>
-
(instancetype)initWithViewController:(UIViewController
*)vc;
-
(void)handleControllerPop:(UIPanGestureRecognizer
*)recognizer;
-
(UIPercentDrivenInteractiveTransition
*)interactivePopTransition;
@endNavigationInteractiveTransition.m#import
"NavigationInteractiveTransition.h"
#import
"PopAnimation.h"
@interface
NavigationInteractiveTransition
()
@property
(nonatomic,
weak)
UINavigationController
*vc;
@property
(nonatomic,
strong)
UIPercentDrivenInteractiveTransition
*interactivePopTransition;
@property(nonatomic,
strong)
UIImageView
*barImageView;
@end
@implementation
NavigationInteractiveTransition
-
(instancetype)initWithViewController:(UIViewController
*)vc
{
self
=
[super
init];
if
(self)
{
self.vc
=
(UINavigationController
*)vc;
self.vc.delegate
=
self;
}
return
self;
}
/**
*
我們把用戶的每次Pan手勢(shì)操作作為一次pop動(dòng)畫(huà)的執(zhí)行
*/
-
(void)handleControllerPop:(UIPanGestureRecognizer
*)recognizer
{
/**
*
interactivePopTransition就是我們說(shuō)的方法2返回的對(duì)象,我們需要更新它的進(jìn)度來(lái)控制Pop動(dòng)畫(huà)的流程,我們用手指在視圖中的位置與視圖寬度比例作為它的進(jìn)度。
*/
CGFloat
progress
=
[recognizer
translationInView:recognizer.view].x
/
recognizer.view.bounds.size.width;
[self.vc.navigationBar
setBackgroundImage:[UIImage
new]
forBarMetrics:UIBarMetricsDefault];
self.vc.navigationBar.shadowImage
=
[UIImage
new];
self.barImageView
=
self.vc.navigationBar.subviews.firstObject;
CGFloat
alpha
=
[[[NSUserDefaults
standardUserDefaults]
objectForKey:@"_alpha"]
floatValue];
self.barImageView.alpha
=
1
-
progress
>
alpha
?
alpha
:
1
-
progress;
//
NSLog(@"===progress==%.2f",progress);
/**
*
穩(wěn)定進(jìn)度區(qū)間,讓它在0.0(未完成)~1.0(已完成)之間
*/
progress
=
MIN(1.0,
MAX(0.0,
progress));
if
(recognizer.state
==
UIGestureRecognizerStateBegan)
{
/**
*
手勢(shì)開(kāi)始,新建一個(gè)監(jiān)控對(duì)象
*/
eractivePopTransition
=
[[UIPercentDrivenInteractiveTransition
alloc]
init];
/**
*
告訴控制器開(kāi)始執(zhí)行pop的動(dòng)畫(huà)
*/
[self.vc
popViewControllerAnimated:YES];
}
else
if
(recognizer.state
==
UIGestureRecognizerStateChanged)
{
/**
*
更新手勢(shì)的完成進(jìn)度
*/
[eractivePopTransition
updateInteractiveTransition:progress];
}
else
if
(recognizer.state
==
UIGestureRecognizerStateEnded
||
recognizer.state
==
UIGestureRecognizerStateCancelled)
{
/**
*
手勢(shì)結(jié)束時(shí)如果進(jìn)度大于一半,那么就完成pop操作,否則重新來(lái)過(guò)。
*/
if
(progress
>
0.5)
{
[eractivePopTransition
finishInteractiveTransition];
self.barImageView.alpha
=
0;;
}
else
{
[eractivePopTransition
cancelInteractiveTransition];
}
eractivePopTransition
=
nil;
}
}
-
(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController
*)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController
*)fromVC
toViewController:(UIViewController
*)toVC
{
/**
*
方法1中判斷如果當(dāng)前執(zhí)行的是Pop操作,就返回我們自定義的Pop動(dòng)畫(huà)對(duì)象。
*/
if
(operation
==
UINavigationControllerOperationPop)
return
[[PopAnimation
alloc]
init];
return
nil;
}
-
(id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController
*)navigationController
interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
/**
*
方法2會(huì)傳給你當(dāng)前的動(dòng)畫(huà)對(duì)象animationController,判斷如果是我們自定義的Pop動(dòng)畫(huà)對(duì)象,那么就返回interactivePopTransition來(lái)監(jiān)控動(dòng)畫(huà)完成度。
*/
if
([animationController
isKindOfClass:[PopAnimation
class]])
return
eractivePopTransition;
return
nil;
}
@endPopAnimation.h#import
<Foundation/Foundation.h>
#import
<UIKit/UIKit.h>
@interface
PopAnimation
:
NSObject
<UIViewControllerAnimatedTransitioning>
@endPopAnimation.m#import
"PopAnimation.h"
@interface
PopAnimation
()
@property
(nonatomic,
strong)
id
<UIViewControllerContextTransitioning>
transitionContext;
@end
@implementation
PopAnimation
-
(NSTimeInterval)transitionDuration:(id
<UIViewControllerContextTransitioning>)transitionContext
{
//這個(gè)方法返回動(dòng)畫(huà)執(zhí)行的時(shí)間
return
0.25;
}
/**
*
transitionContext你可以看作是一個(gè)工具,用來(lái)獲取一系列動(dòng)畫(huà)執(zhí)行相關(guān)的對(duì)象,并且通知系統(tǒng)動(dòng)畫(huà)是否完成等功能。
*/
-
(void)animateTransition:(id
<UIViewControllerContextTransitioning>)transitionContext
{
/**
*
獲取動(dòng)畫(huà)來(lái)自的那個(gè)控制器
*/
UIViewController
*fromViewController
=
[transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
/**
*
獲取轉(zhuǎn)場(chǎng)到的那個(gè)控制器
*/
UIViewController
*toViewController
=
[transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
/**
*
轉(zhuǎn)場(chǎng)動(dòng)畫(huà)是兩個(gè)控制器視圖時(shí)間的動(dòng)畫(huà),需要一個(gè)contain
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 ISO 18650-2:2025 EN Building construction machinery and equipment - Concrete mixers - Part 2: Procedure for examination of mixing efficiency
- 臨沂2025年山東臨沂職業(yè)學(xué)院引進(jìn)高層次人才67人筆試歷年參考題庫(kù)附帶答案詳解
- 二零二五夫妻忠誠(chéng)協(xié)議保證書(shū)
- 第三人房產(chǎn)抵押擔(dān)保合同
- 餐飲合伙協(xié)議合同
- 二零二五駕校學(xué)員培訓(xùn)協(xié)議合同書(shū)范例
- 二零二五版離婚后子女撫養(yǎng)權(quán)歸屬協(xié)議書(shū)
- 二零二五版餐飲雙方合作協(xié)議書(shū)
- 二零二五版企業(yè)股權(quán)轉(zhuǎn)讓及增資協(xié)議書(shū)
- 2025年個(gè)人購(gòu)房合同示范文本
- 2024年思政考試準(zhǔn)備試題及答案
- 2024年婁底市公安局警務(wù)輔助人員招聘考試真題
- 總經(jīng)理聘任合同模板7篇
- PLC應(yīng)用技術(shù)課件 任務(wù)6. S7-1200 PLC控制電動(dòng)機(jī)正反轉(zhuǎn)
- 2024年時(shí)事政治熱點(diǎn)題庫(kù)200道含完整答案(必刷)
- 血液形態(tài)學(xué)圖譜考核(ISO15189現(xiàn)場(chǎng)考核)
- 太平哨水利樞紐引水式水電站設(shè)計(jì)
- YC/T 425-2011煙用紙張尺寸的測(cè)定非接觸式光學(xué)法
- GB/T 6109.2-2008漆包圓繞組線第2部分:155級(jí)聚酯漆包銅圓線
- 99S203 消防水泵接合器安裝圖集
- GB∕T 21489-2018 散糧汽車卸車裝置
評(píng)論
0/150
提交評(píng)論