【移动应用开发技术】怎么在iOS中实现一个透明导航栏_第1页
【移动应用开发技术】怎么在iOS中实现一个透明导航栏_第2页
【移动应用开发技术】怎么在iOS中实现一个透明导航栏_第3页
【移动应用开发技术】怎么在iOS中实现一个透明导航栏_第4页
【移动应用开发技术】怎么在iOS中实现一个透明导航栏_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么在iOS中实现一个透明导航栏

这篇文章给大家介绍怎么在iOS中实现一个透明导航栏,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。首先在需要设置导航栏透明的页面的viewDidload中写上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;

//设置状态栏

[[UIApplication

sharedApplication]

setStatusBarStyle:UIStatusBarStyleLightContent];

//设置标题颜色

self.navigationController.navigationBar.titleTextAttributes

=

@{NSForegroundColorAttributeName

:

[UIColor

clearColor]};在scrollViewDidScroll代理方法中-(void)scrollViewDidScroll:(UIScrollView

*)scrollView

{

CGFloat

offset

=

scrollView.contentOffset.y;

//根据自己需要设置(136)的大小

CGFloat

alpha

=

offset

/

136;

_barImageView.alpha

=

alpha;

//记录下当前的透明度,在返回当前页面时需要

_alpha

=

alpha;

[[NSUserDefaults

standardUserDefaults]

setObject:[NSNumber

numberWithFloat:alpha]

forKey:@"_alpha"];

//设置标题的透明度

self.navigationController.navigationBar.titleTextAttributes

=

@{NSForegroundColorAttributeName

:

[UIColor

colorWithWhite:0

alpha:alpha]};

}当前页的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的下一个页面需要什么操作呢,我们需要在这个页面显示正常的nav并且禁掉系统的手势pop,自己写一个pop手势,以方便我们拿到pop滑动时的偏移量,在做的时候使用了两个类,在最后会有源码贴出B.m须遵守UIGestureRecognizerDelegate,并导入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

{

//记录当前是是否是通过手势滑动回去

[[NSUserDefaults

standardUserDefaults]

setObject:[NSNumber

numberWithBool:YES]

forKey:@"isGesturePop"];

/**

*

这里有两个条件不允许手势执行,1、当前控制器为根控制器;2、如果这个push、pop动画正在执行(私有属性)

*/

return

self.navigationController.viewControllers.count

!=

1

&&

![[self.navigationController

valueForKey:@"_isTransitioning"]

boolValue];

}需要依赖的两个类源码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手势操作作为一次pop动画的执行

*/

-

(void)handleControllerPop:(UIPanGestureRecognizer

*)recognizer

{

/**

*

interactivePopTransition就是我们说的方法2返回的对象,我们需要更新它的进度来控制Pop动画的流程,我们用手指在视图中的位置与视图宽度比例作为它的进度。

*/

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);

/**

*

稳定进度区间,让它在0.0(未完成)~1.0(已完成)之间

*/

progress

=

MIN(1.0,

MAX(0.0,

progress));

if

(recognizer.state

==

UIGestureRecognizerStateBegan)

{

/**

*

手势开始,新建一个监控对象

*/

eractivePopTransition

=

[[UIPercentDrivenInteractiveTransition

alloc]

init];

/**

*

告诉控制器开始执行pop的动画

*/

[self.vc

popViewControllerAnimated:YES];

}

else

if

(recognizer.state

==

UIGestureRecognizerStateChanged)

{

/**

*

更新手势的完成进度

*/

[eractivePopTransition

updateInteractiveTransition:progress];

}

else

if

(recognizer.state

==

UIGestureRecognizerStateEnded

||

recognizer.state

==

UIGestureRecognizerStateCancelled)

{

/**

*

手势结束时如果进度大于一半,那么就完成pop操作,否则重新来过。

*/

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中判断如果当前执行的是Pop操作,就返回我们自定义的Pop动画对象。

*/

if

(operation

==

UINavigationControllerOperationPop)

return

[[PopAnimation

alloc]

init];

return

nil;

}

-

(id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController

*)navigationController

interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController

{

/**

*

方法2会传给你当前的动画对象animationController,判断如果是我们自定义的Pop动画对象,那么就返回interactivePopTransition来监控动画完成度。

*/

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

{

//这个方法返回动画执行的时间

return

0.25;

}

/**

*

transitionContext你可以看作是一个工具,用来获取一系列动画执行相关的对象,并且通知系统动画是否完成等功能。

*/

-

(void)animateTransition:(id

<UIViewControllerContextTransitioning>)transitionContext

{

/**

*

获取动画来自的那个控制器

*/

UIViewController

*fromViewController

=

[transitionContext

viewControllerForKey:UITransitionContextFromViewControllerKey];

/**

*

获取转场到的那个控制器

*/

UIViewController

*toViewController

=

[transitionContext

viewControllerForKey:UITransitionContextToViewControllerKey];

/**

*

转场动画是两个控制器视图时间的动画,需要一个contain

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论