【转载请注明出处】

iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势。支持此手势的是UINavigationController中新增的属性

interactivePopGestureRecognizer,即右滑返回只支持以UINavigationController为容器的ViewController间切换,要想在自定义容器中使用,需要一些额外的工作。

基本地,控制ViewController是否启用右滑返回,只需要这样:

1 self.navigationController.interactivePopGestureRecognizer.enabled = YES;

默认情况下enabled为YES。

在实际使用中,遇到了一些问题,整理如下: 
1、自定义返回按钮后,右滑返回失效;

解决方案:比较直观的办法是在自定义返回按钮时,使用backBarButtonItem:

1     UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
2 //some initialize code here...
3 UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
4 self.navigationItem.leftBarButtonItem = barItem; //not working
5 self.navigationItem.backBarButtonItem = barItem; //serve well

P.S:关于backBarButtonItem和leftBarButtonItem的区别:

http://www.cocoachina.com/ask/questions/show/97110

但这样无法支持左上角多个按钮的情况。考虑到 interactivePopGestureRecognizer也有delegate属性, 替换默认的 self . navigationController .interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。在主ViewController中:

1   self.navigationController.interactivePopGestureRecognizer.delegate = self;
 1   - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
2   {
3   if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
4   {
5   return NO;
6   }
7   else
8   {
9   return YES;
10   }
11   }

如此做的好处是可以在主ViewController中配置栈中所有ViewController右滑返回的开启,而不需要在各个ViewController中分别设置enabled。

值得注意的是:在替换了delegate之后,必须在gestureRecognizerShouldBegin:中设置某ViewController A开启右滑返回,同时在A中未设置interactivePopGestureRecognizer.enabled = NO,右滑返回才会开启,即二者中任一为NO,右滑返回都处于关闭状态。

2、主界面(UINavigationController栈中的第一个ViewController)默认也是开启右滑返回的。若在主界面上右滑,不会有动作执行。但此时想进入下一级ViewController(如点击tableView中某一行),切换动画却没有出现。切回桌面再进入应用,发现直接进入了下一级ViewController。

解决方案:这个问题是在最初试验右滑返回的使用方式时出现的。在使用自定义返回按钮的ViewController中

1 self.navigationController.interactivePopGestureRecognizer.delegate = self;

解决解决问题1的同时,造成了问题2。和1中相似,都是在替换了默认的delegate之后,interactivePopGestureRecognizer就能调用自定义的返回方法了。具体原因尚不清楚,待更新【Mark】。

3、在使用右滑返回拖动到一半时,有时会在导航栏上看到三个排成一行的小蓝点。

解决方案:原因不明,解决方案不明。

P.S:在一个帖子上看到一个办法:

1   self.navigationItem.title = @"";

可以隐藏小蓝点,但由于小蓝点非必现,在不明究竟的情况下很难说是否有效。

帖子链接: http://www.tuicool.com/articles/FB3IJ3

(1)在工程中查看, self . navigationController .interactivePopGestureRecognizer.delegate实际上是一个

_UINavigationInteractiveTransition实例,该类声明如下:

 1   @class UIScreenEdgePanGestureRecognizer;
2
3   @interface _UINavigationInteractiveTransition : _UINavigationInteractiveTransitionBase {
4   UIScreenEdgePanGestureRecognizer *_edgePanRecognizer;
5   }
6
7   @property(readonly) UIScreenEdgePanGestureRecognizer * screenEdgePanGestureRecognizer;
8
9   - (void)_configureNavigationGesture;
10   - (BOOL)_gestureRecognizer:(id)arg1 shouldBeRequiredToFailByGestureRecognizer:(id)arg2;
11   - (void)dealloc;
12   - (BOOL)gestureRecognizer:(id)arg1 shouldReceiveTouch:(id)arg2;
13   - (BOOL)gestureRecognizer:(id)arg1 shouldRecognizeSimultaneouslyWithGestureRecognizer:(id)arg2;
14   - (BOOL)gestureRecognizerShouldBegin:(id)arg1;
15   - (id)gestureRecognizerView;
16   - (id)initWithViewController:(id)arg1 animator:(id)arg2;
17   - (id)screenEdgePanGestureRecognizer;
18   - (void)setNotInteractiveTransition;
19   - (void)startInteractiveTransition;
20
21   @end

可以看到,委托的内部,实际上是一个UIScreenEdgePanGestureRecognizer实例在起作用,它是iOS7中引入的一个新类,用于支持某些情况下ViewController间切换的初始化。apple官方文档中对其的描述很少,如下:

A  UIScreenEdgePanGestureRecognizer  looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions.

After creating a screen edge pan gesture recognizer, assign an appropriate value to the edges  property before attaching the gesture recognizer to your view. You use this property to specify from which edges the gesture may start. This gesture recognizer ignores any touches beyond the first touch.

要在自定义的ViewController容器中支持右滑返回,可能就需要用到它。

(2)目前不少应用还是用的iOS 6.1 SDK,而许多iOS7的用户对右滑返回的需求非常迫切,因此在iOS 6.1SDK下模拟右滑返回在短时间内是有必要的,以下是一个通过在push时截取上级ViewController界面为UIImage作为下一级ViewController的背景的一种实现方式:

作者的本意似乎并不要要模拟右滑返回,但稍作修改就能在结构比较简单的应用中使用,以下是链接:

https://github.com/vinqon/MultiLayerNavigation

P.S:对于一些特殊的需求,如在有ScrollView的界面上(比如浏览照片)模拟右滑返回,当滑动到最左边时即执行右滑返回,该类无法满足,待处理【Mark】。

1、UIScreenEdgePanGestureRecognizer Class Reference

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreenEdgePanGestureRecognizer_class/Reference/Reference.html#//apple_ref/occ/cl/UIScreenEdgePanGestureRecognizer

2、_UINavigationInteractiveTransition.h

https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/_UINavigationInteractiveTransition.h

3、自定义返回按钮时,iOS7手势返回遇到的问题

http://www.tuicool.com/articles/FB3IJ3

http://www.tuicool.com/articles/vMfAVv

iOS7滑动返回的更多相关文章

  1. iOS7下滑动返回与ScrollView共存二三事

    [转载请注明出处] = =不是整篇复制就算注明出处了亲... iOS7下滑动返回与ScrollView共存二三事 [前情回顾] 去年的时候,写了这篇帖子iOS7滑动返回.文中提到,对于多页面结构的应用 ...

  2. iOS6下实现滑动返回

    [转载请注明出处] 之前在看iOS7滑动返回时,发现了一个iOS6 SDK下的第三方实现,今天偶然间发现了作者在其博客上对该实现的一些心得,读来深觉之前的思考太过肤浅,许多实际的问题没有考虑到.帖子链 ...

  3. 禁用ios7 手势滑动返回功能

    禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...

  4. iOS7上leftBarButtonItem无法实现滑动返回的完美解决方案

    今天遇到了在iOS7上使用leftBarButtonItem却无法响应滑动返回事件的问题,一番谷歌,最后终于解决了,在这里把解决方案分享给大家. 在iOS7之前的系统,如果要自定义返回按钮,直接设置b ...

  5. UINavigationController侧滑滑动返回 卡死问题

    UINavigationController滑动返回,有需要的朋友可以参考下. 最近做了UINavigationController的滑动返回(IOS7及以后系统默认支持的), 主要分成以下几步以及碰 ...

  6. iOS之手势滑动返回功能-b

    iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...

  7. iOS之手势滑动返回功能

    iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...

  8. iOS 如何设置导航的滑动返回手势, 和系统饿一样

    iOS 7 滑动返回那些事儿 2014/05/17 Wei .entry-meta .entry-header 在智能机越来越普及,屏幕越做越大的当下,滑动返回手势已经成为了一个应用的标配功能,甚至可 ...

  9. iOS 自定义返回按钮,保留系统滑动返回

    原文链接 自定义返回按钮保留系统滑动返回手势.gif 1.简介 使用苹果手机,最喜欢的就是用它的滑动返回.作为一个开发者,我们在编写很多页面的时候,总是会因为这样那样的原因使得系统的滑动返回不可用.使 ...

随机推荐

  1. ASP.NET 访问 MySql

    1. 首先需要安装mysql, 脚本之家下载地址: http://www.jb51.net/softs/2193.html 或者去mysql.com官网都可以,一路next,安装好后,有个简单配置,提 ...

  2. maven新建Spring MVC + MyBatis + Oracle的Web项目中pom.xml文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. [itint5]合并K个有序链表

    merge sort,leet code里面曾经做过.但一开始没这么写,遍历来做,效率n*k了,用了merge sort后,变成logn*k. 用了dummy node.同时要注意size为0的情况. ...

  4. Android 去除EditText边框,添加下划线,

    首先:重写EditText //请在这里添加您的包名 import android.content.Context; import android.graphics.Canvas; import an ...

  5. python的常用概念

    常用的概念 主体字符串 主体列表 内置函数和方法的区别 映射表 引用 迭代器: 1. 字典:单步遍历迭代器 2. 文件:逐行读取的迭代器

  6. 使用PHP抓取网站ico图标

    网站许久没用更新,以后会经常更新,本次分享一个使用PHP抓取网站ico的程序,提供一个网站列表后对网站的ico进行下载抓取,具体代码如下: <?php /** * 更新热站ico * gao 2 ...

  7. NFC(13)使用Android Beam技术传输文件

    注意 Android Beam技术传输文件时nfc只负责连接两个手机,而传输文件实际是用蓝牙模块.且目前接收文件功能只是系统完成,不用自写个接收程序. 传输文件相关的重要api 从Android4.1 ...

  8. C语言一维数组中的数据随机排列

    #include <stdio.h>#include <stdlib.h> void randomlize(int *a, int n){        int i = 0,j ...

  9. WPF程序中处理Windows消息

    首先通过WindowInteropHelper类,我们可以获取WPF Window的Handle. WindowInteropHelper helper = new WindowInteropHelp ...

  10. POJ 3678 Katu Puzzle (2-SAT,常规)

    题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...