iOS--无需写代码实现全屏滑动手势

一、由于公司项目中需要实现全屏返回pop手势的功能,于是想着去github上找找看有没有现成的,搜了挺多的,发现都不能满足我的需求。

需求:1、支持全屏手势,滑到不到一半的时候要返回(产品的要求,说要灵敏一点)
2、在有些页面需要禁用全屏的滑动手势

github上也有大家用的常用的FDFullscreenPopGesture,不过不能支持定制滑动返回比例,于是自己撸一个。

二、Talk is cheap Show me the code

废话不多说,先看看实现的大概效果。

iOS--无需写代码实现全屏滑动手势_第1张图片
YJNavigationFullGesture.gif

从iOS7系统开始就有滑动返回手势,不过不是全屏的,只有在边缘的时候才能触发,FDFullscreenPopGesture的作者就是调用系统的私有api去实现的。
我的实现思路很简单:就是自定义交互动画和pop动画去实现,核心代码如下:

// 获取本身的手势并禁用
UIGestureRecognizer *geture = self.interactivePopGestureRecognizer;

geture.enabled = NO;

//自定义pan手势

UIView *getureView = geture.view;
UIPanGestureRecognizer *popGesture = [[UIPanGestureRecognizer alloc] init];
popGesture.delegate = self.yj_gestureDelegate;
popGesture.maximumNumberOfTouches = 1;
[getureView addGestureRecognizer:popGesture];

// 自定义的交互动画
self.yj_interactiveTransition = [[YJInteractiveTransition alloc] initWithViewController:self];

self.yj_interactiveTransition.popProgross = self.yj_popProgress?self.yj_popProgress:0.5;

[popGesture addTarget:self.yj_interactiveTransition action:@selector(handlePopGesture:)];

我开始的思路是写了个UINavigationController的子类去实现,后面感觉继承不是特别的好,于是就写了UINavigationController分类去实现。这样的话只要把我的文件拖到工程中,不需要写任何代码就能支持全屏手势了。

1、如果需要自定pop滑动动比例需要

#import "UINavigationController+YJFullGesture.h"

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];

// 设置滑动比例,不设置的话默认为0.5,
navi.yj_popProgress = 0.35;



self.window.rootViewController = navi;

[self.window makeKeyAndVisible];

2、禁用手势的话,在需要push的controller来设置,#import "UIViewController+YJFullGesture.h"

// 关闭全屏手势,默认为开启
self.yj_disabledFullGesture = YES;

具体实现去看YJNavigationFullGesture,如果需要用到的话下载下来拖入工程即可,同时也支持cocopods安装。

你可能感兴趣的:(iOS--无需写代码实现全屏滑动手势)