从 iOS 7.0 后苹果自带了侧滑返回手势功能interactivePopGestureRecognizer,但是有时我们自定义返回按钮或者隐藏了导航栏,侧滑返回就会失效,而它又是我们的应用使用起来更加人性化必不可少的一部分,如何重现它呢?请往下看~

Demo 展示效果

先看官方介绍//The gesture recognizer responsible for popping the

top view controller off the navigation stack. (read-only)

The navigation controller installs this gesture

recognizer on its view and uses it to pop the topmost

view controller off the navigation stack. You can use

this property to retrieve the gesture recognizer and tie

it to the behavior of other gesture recognizers in your

user interface. When tying your gesture recognizers

together, make sure they recognize their gestures

simultaneously to ensure that your gesture recognizers

are given a chance to handle the event.

(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

介绍和属性中我们可以知道interactivePopGestureRecognizer是负责把Navigation 栈中最上面的View Controller推出去。而且它只能在iOS7.0及以上可以使用,并且禁止在TVOS中使用!

How to use

由于interactivePopGestureRecognizer是一种手势,因此我们在使用它时需遵循UIGestureRecognizerDelegateProtocol.

例如:

#import ZHRootViewController : UIViewController

我们需要把interactivePopGestureRecognizer使能打开,并设置委托对象,当然在这之前别忘了对系统版本进行判断,因为它只能在iOS7.0及以上使用。

For example:

- (void)viewDidLoad

{

[super viewDidLoad];

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

{

self.navigationController.interactivePopGestureRecognizer.enabled = YES; // 手势有效设置为YES 无效为NO

self.navigationController.interactivePopGestureRecognizer.delegate = self; // 手势的代理设置为self

}

// Do any additional setup after loading the view.

}

显示或者隐藏NavigationBar 注意这里需要在函数-(void)viewWillAppear:(BOOL)animated 中调用

Like this:

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:NO animated:animated];//No 为显示Navigationbar Yes 为隐藏

}

做完这些运行后动画效果有了,是不是就完事了呢?No No

你想一想如果已经到Navigation 栈中最底一个Controller时我们还允许侧滑么,显然这时我们应该使侧滑手势失效!

像这样:

-(void)viewDidAppear:(BOOL)animated

{

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

{

if (self.navigationController.viewControllers.count==1) {

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}

}

}

或者:

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

if (self.navigationController.viewControllers.count == 1) {

return NO;

}else{

return YES;

}

}

到现在我们应该做完了吧? No No

假如我们的View是一个可滑动的View呢?例如ScrollView,TableView,CollectionView。由于它们自带了panGestureRecognizer 所以它们一起使用时可能会产生冲突,如何避免呢?我们可以设置一个手势依赖关系!

Like this:

NSArray *gestureArray = self.navigationController.view.gestureRecognizers;//获取所有的手势

//当是侧滑手势的时候设置panGestureRecognizer需要UIScreenEdgePanGestureRecognizer失效才生效即可

for (UIGestureRecognizer *gesture in gestureArray) {

if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {

[self.tableView.panGestureRecognizer requireGestureRecognizerToFail:gesture];

}

}

这里你也许会疑惑为什么我们上面用的是UIScreenEdgePanGestureRecognizer而不是interactivePopGestureRecognizer?

我们先打印self.navigationController.interactivePopGestureRecognizer结果为:

delaysTouchesBegan = YES;

view = ;

target=

target=<_uinavigationinteractivetransition>)>>

打印结果中我们看到了UIScreenEdgePanGestureRecognizer:说明interactivePopGestureRecognizer其实由UIScreenEdgePanGestureRecognizer控制并执行handleNavigationTransition:其代理对象为_UINavigationInteractiveTransition,所以判断中我们使用了UIScreenEdgePanGestureRecognizer.

扩展

我们可以根据UIScreenEdgePanGestureRecognizer,handleNavigationTransition:来自定义我们自己的侧滑返回,这章先讲诉到这里!

Demo

ios 侧滑返回停顿_如何使用 iOS 系统自带侧滑返回功能相关推荐

  1. ios单应用模式_如何为iOS 13暗模式设置应用

    ios单应用模式 Apple launched the much-awaited iOS 13 updates globally on September 19 across all iPhones ...

  2. ios 访问网络共享_如何从iOS设备无线访问网络共享

    ios 访问网络共享 Syncing files between your computer and your iOS device via iTunes can be tedious and req ...

  3. 拦截httpservlet返回码_设计 API 接口,实现统一格式返回

    来源:老顾聊技术 前言 在移动互联网,分布式.微服务盛行的今天,现在项目绝大部分都采用的微服务框架,前后端分离方式.一般系统的大致整体架构图如下: 接口交互 前端和后端进行交互,前端按照约定请求URL ...

  4. ios 高德获取定位_解决ios11不支持高德地图API定位功能的方法

    在 iOS 11 系统上访问JS API定位业务失败怎么解决? 苹果新发的 iOS 11 操作系统的一大特性是对 http 形式访问页面的限制变得非常严格(相比iOS 10 和 iOS 9).高德提供 ...

  5. ios 高德获取定位_更新日志-iOS 定位SDK | 高德地图API

    高德地图iOS 定位 SDK V2.6.7            2020-08-28 1.适配iOS14定位权限:新增"模糊定位"权限下的兼容策略: 2.修复bug,提升性能和稳 ...

  6. ios 长按 放大镜_如何在iOS 10中将iPhone的相机用作放大镜

    ios 长按 放大镜 If you've ever found yourself wishing you had a magnifying glass with you, iOS 10 can now ...

  7. ios 裁剪圆形头像_详解iOS 裁剪圆形图像并显示(类似于微信头像)

    本文主要讲解如何从照片库选择一张照片后将其裁剪成圆形头像并显示,类似于微信头像那种模式. 本文的方法也适用于当时拍照获取的图像,方法类似,所以不再赘述. 本文主要是在iOS 10环境下使用,此时如果要 ...

  8. 移动应用ios和网页应用_如何在iOS上一次移动多个应用

    移动应用ios和网页应用 Apple doesn't really believe in detailed instruction manuals, so some handy tricks slip ...

  9. ios 拍照 实现 连拍_如何在iOS设备上使用连拍模式拍照

    ios 拍照 实现 连拍 We're sure you've tried to capture that perfect moment with your camera and you're just ...

最新文章

  1. Linux 终端推荐 Terminator
  2. K8S部署工具:KubeOperator集群部署
  3. 【转载】Linux截图工具
  4. 大数据WEB阶段(六)MySql详解(二)
  5. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析
  6. 工业POE供电交换机在安防应用中的优势有哪些?
  7. [Leetcode][第35题][JAVA][搜索插入位置][二分法]
  8. JavaScript重难点解析2(立即执行函数IIFE,this关键字)
  9. PS VR发售临近,索尼的VR影视内容也不远了
  10. 团队作业(二):项目选题
  11. 画图必备numpy函数
  12. [swift] LeetCode 49. Group Anagrams
  13. Django Rest Framework中文文档:Serializer relations
  14. 基本磁盘转换动态磁盘,再转换为基本磁盘,分区数据丢失
  15. journalctl命令使用
  16. C语言—各种数据类型间的混合运算
  17. Python用socket、多线程实现一对一聊天室
  18. 【如何7天写完一篇发明专利】
  19. proe安装后 显示服务器未连接,proe许可证服务器主机名
  20. python语言基础-字典

热门文章

  1. 硬件技术经验知识点:1分钟了解RC、RL、RLC电路原理
  2. python调用函数必须带括号吗_Python笔记:调用函数,带扩号和和不带括号的区别...
  3. jdk19下载与安装教程(win10)超详细
  4. 传智健康项目day01
  5. 太蔟---于丹的脊续
  6. 树莓派python编程优点_树莓派为什么会使用python编程?为什么有时python运行效率不高?...
  7. Greasy Fork,这里是一个提供用户脚本的网站。
  8. 1、vert.x的特性
  9. java如何打开hprof_hprof是什么文件?如何打开hprof?
  10. Chrome devtool调试出现404的解决办法