再谈iOS 7的手势滑动返回功能
现在使用默认模板创建的iOS App都支持手势返回功能,如果导航栏的返回按钮是自定义的那么则会失效,也可以参考这里手动设置无效。
- if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
- self.navigationController.interactivePopGestureRecognizer.enabled = NO;
- }
如果是因为自定义导航按钮而导致手势返回失效,那么可以在NavigationController的viewDidLoad函数中添加如下代码:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- __weak typeof (self) weakSelf = self;
- if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
- self.interactivePopGestureRecognizer.delegate = weakSelf;
- }
- }
这样写了以后就可以通过手势滑动返回上一层了,但是如果在push过程中触发手势滑动返回,会导致导航栏崩溃(从日志中可以看出)。针对这个问题,我们需要在pop过程禁用手势滑动返回功能:
- - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
- {
- // fix 'nested pop animation can result in corrupted navigation bar'
- if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
- self.interactivePopGestureRecognizer.enabled = NO;
- }
- [super pushViewController:viewController animated:animated];
- }
- - (void)navigationController:(UINavigationController *)navigationController
- didShowViewController:(UIViewController *)viewController
- animated:(BOOL)animated
- {
- if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
- navigationController.interactivePopGestureRecognizer.enabled = YES;
- }
- }
除了使用系统默认的动画,还可以使用自定义过渡动画(丰满的文档):
- - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
- animationControllerForOperation:(UINavigationControllerOperation)operation
- fromViewController:(UIViewController *)fromVC
- toViewController:(UIViewController *)toVC
- {
- if (operation == UINavigationControllerOperationPop) {
- if (self.popAnimator == nil) {
- self.popAnimator = [WQPopAnimator new];
- }
- return self.popAnimator;
- }
- return nil;
- }
- - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
- interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
- {
- return self.popInteractionController;
- }
- #pragma mark -
- - (void)enablePanToPopForNavigationController:(UINavigationController *)navigationController
- {
- UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]
- initWithTarget:self
- action:@selector(didPanToPop:)];
- //[left2rightSwipe setDelegate:self];
- [left2rightSwipe setEdges:UIRectEdgeLeft];
- [navigationController.view addGestureRecognizer:left2rightSwipe];
- self.popAnimator = [WQPopAnimator new];
- self.supportPan2Pop = YES;
- }
- - (void)didPanToPop:(UIPanGestureRecognizer *)panGesture
- {
- if (!self.supportPan2Pop) return ;
- UIView *view = self.navigationController.view;
- if (panGesture.state == UIGestureRecognizerStateBegan) {
- self.popInteractionController = [UIPercentDrivenInteractiveTransition new];
- [self.navigationController popViewControllerAnimated:YES];
- } else if (panGesture.state == UIGestureRecognizerStateChanged) {
- CGPoint translation = [panGesture translationInView:view];
- CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));
- [self.popInteractionController updateInteractiveTransition:d];
- } else if (panGesture.state == UIGestureRecognizerStateEnded) {
- if ([panGesture velocityInView:view].x > 0) {
- [self.popInteractionController finishInteractiveTransition];
- } else {
- [self.popInteractionController cancelInteractiveTransition];
- }
- self.popInteractionController = nil;
- }
- }
如下这个代理方法是用来提供一个非交互式的过渡动画的:
- - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
- animationControllerForOperation:(UINavigationControllerOperation)operation
- fromViewController:(UIViewController *)fromVC
- toViewController:(UIViewController *)toVC
- {
- if (operation == UINavigationControllerOperationPop) {
- if (self.popAnimator == nil) {
- self.popAnimator = [WQPopAnimator new];
- }
- return self.popAnimator;
- }
- return nil;
- }
而下面这个代理方法则是提供交互式动画:
- - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
- interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
- {
- return self.popInteractionController;
- }
这两个组合起来使用。首先,我们需要有个动画:
- @interface WQPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>
- @end
- #import "WQPopAnimator.h"
- @implementation WQPopAnimator
- - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
- {
- return 0.4;
- }
- - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
- {
- UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
- UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
- [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
- __block CGRect toRect = toViewController.view.frame;
- CGFloat originX = toRect.origin.x;
- toRect.origin.x -= toRect.size.width / 3;
- toViewController.view.frame = toRect;
- [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
- CGRect fromRect = fromViewController.view.frame;
- fromRect.origin.x += fromRect.size.width;
- fromViewController.view.frame = fromRect;
- toRect.origin.x = originX;
- toViewController.view.frame = toRect;
- } completion:^(BOOL finished) {
- [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
- }];
- }
- @end
其次,交互式动画是通过
- UIPercentDrivenInteractiveTransition
来维护的,在滑动过程中根据滑动距离来进行更新:
- } else if (panGesture.state == UIGestureRecognizerStateChanged) {
- CGPoint translation = [panGesture translationInView:view];
- CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));
- [self.popInteractionController updateInteractiveTransition:d];
当手势结束时要做出收尾动作:
- } else if (panGesture.state == UIGestureRecognizerStateEnded) {
- if ([panGesture velocityInView:view].x > 0) {
- [self.popInteractionController finishInteractiveTransition];
- } else {
- [self.popInteractionController cancelInteractiveTransition];
- }
- self.popInteractionController = nil;
- }
同样地,自定义的动画也会有上面提到的导航栏崩溃问题,也可以通过类似的方法来解决:
- - (void)navigationController:(UINavigationController *)navigationController
- didShowViewController:(UIViewController *)viewController
- animated:(BOOL)animated
- {
- if (viewController == self.navigationController.pushingViewController) {
- self.supportPan2Pop = YES;
- self.navigationController.pushingViewController = nil;
- }
补充:位于当前navgationController的第一个([0])viewController时需要设置手势代理,不响应。
再谈iOS 7的手势滑动返回功能的更多相关文章
- iOS 7的手势滑动返回
如今使用默认模板创建的iOS App都支持手势返回功能,假设导航栏的返回button是自己定义的那么则会失效,也能够參考这里手动设置无效. if ([self.navigationController ...
- 禁用ios7 手势滑动返回功能
禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...
- iOS之手势滑动返回功能-b
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- iOS之手势滑动返回功能
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- iOS 应用全部添加滑动返回
if ([self class] == [HomeViewController class]||[self class] == [ComprehensivefinanceViewControlle ...
- Android-通过SlidingMenu高仿微信6.2最新版手势滑动返回(二)
转载请标明出处: http://blog.csdn.net/hanhailong726188/article/details/46453627 本文出自:[海龙的博客] 一.概述 在上一篇博文中,博文 ...
- Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)
近期更新了微信版本号到6.2.发现里面有个很好的体验,就是在第二个页面Activity能手势向右滑动返回,在手势滑动的过程中能看到第一个页面,这样的体验很赞,这里高仿了一下. 这里使用的是v4包里面的 ...
- iOS彩票项目--第五天,新特性引导页的封装、返回按钮的自定义、导航控制器的滑动返回以及自定义滑动返回功能
一.上次实现了在AppDelegate中通过判断app版本决定是否进入新特性页面,今天将AppDelegate中的一坨进行了封装.将self.window的根控制器到底应该为新特性界面,还是主页面,封 ...
- UINavigationController实现全屏滑动返回功能
说明: UINavigationController默认在push出的控制器中都有边沿滑动返回功能,但是只能从屏幕左边滑才能返回,若从屏幕中间画并没有效果.下面实现全屏滑动功能. 探究: 系统默认能够 ...
随机推荐
- DRUPAL性能优化【转】
1.启用memcache代替Mysql的缓存表处理缓存数据. 2.添加一个opcode缓存可以让 PHP能够重用前面编译过的代码,这样就会跳过解析和编译.常见的opcode缓存有Alternative ...
- shell脚本中执行mysql 语句,去除warning using a password on the command line interface can be insecure信息
方法二:使用mysql参数的方法 mysql -u$user -p$pass -D $db -e "select host from user;"当然,可以通过将传参的方式来传递 ...
- HBase ProcedureV2 分析
Procedure V2, 是hbase1.1版本引入的一套fault-tolerant的执行multi-steps-job的框架, 目前主要用在Master中, 比如创建表,删除表等操作 新旧比较 ...
- PHP-客户端的IP地址伪造、CDN、反向代理、获取的那些事儿
外界流传的JAVA/PHP服务器端获取客户端IP都是这么取的: 伪代码: 1)ip = request.getHeader("X-FORWARDED-FOR") 可伪造,参 ...
- 我眼中的PageRank算法详解
随着互联网的发展,网络上已有的网页数量庞大,并且每天都会有很多网页发布,如何权衡这些重要度的排名是一个很重要的问题.我们今天就来了解一下PageRank算法. 首先我们要来了解一下图的概念,请看图1. ...
- 网络请求框架---Volley
去年的Google I/O大会为android开发者带来了一个网络请求框架,它的名字叫做Volley.Volley诞生的使命就是让Android的网络请求更快,更健壮,而且它的网络通信的实现是基于Ht ...
- golang中使用mongodb
mgo简介 mongodb官方没有关于go的mongodb的驱动,因此只能使用第三方驱动,mgo就是使用最多的一种. mgo(音mango)是MongoDB的Go语言驱动,它用基于Go语法的简单API ...
- 未设置BufferSize导致FTP下载速度过慢的问题
開始下载前设置BufferSize就可以解决: ftpClient.setBufferSize(1024*1024); 查看commons-net的源代码.能够发现假设未设置该參数.将会一个字节一个字 ...
- Atitit.操作注册表 树形数据库 注册表的历史 java版本类库总结
Atitit.操作注册表 树形数据库 注册表的历史 java版本类库总结 1. 注册表是树形数据库 1 2. 注册表的由来 1 3. Java 操作注册表 2 3.1. 使用Preferences ...
- C语言第十一回合:预处理命令的集中营
C语言第十一回合:预处理命令的集中营 [学习目标] 1. 宏定义 2. 文件包括"处理 3. 条件编译 预处理命令:能够改进程序设计的 ...