你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
首页
热门
推荐
精选
登录
|
注册
自定义全屏返回手势
立即下载
用AI写一个
金额:
2
元
支付方式:
友情提醒:源码购买后不支持退换货
立即支付
我要免费下载
发布时间:2017-11-21
1人
|
浏览:3981次
|
收藏
|
分享
技术:runtime
运行环境:xcode
概述
由于系统提供的返回手势只能在屏幕边缘使用,使用runtime替换方法,用自定义手势打造全屏返回手势。随心所欲的滑
详细
>使用分类,入侵性低,直接拖动到项目中,一句代码都不用写,自动生效 ## 演示效果 ![](/contentImages/image/20171121/RirEcar85u8tQgktUFj.gif) ## 目录结构 ![](/contentImages/image/20171121/UMEWfpJ2T7bfMSP9B9R.jpg) ## 原理 **.h文件** ```objc /// 自定义全屏拖拽返回手势 @property (nonatomic, strong, readonly) UIPanGestureRecognizer *popGestureRecognizer; ``` 定义单个控制器取消手势方法 ```objc - (void)cancleHMFullScreenPopGesRec:(BOOL)cancle; ``` **.m文件** 一、引入头文件``#import
`` 二、定义手势代理类 ```objc // 判断是否是根控制器,如果是,取消手势 if (self.navigationController.viewControllers.count <= 1) { return NO; } // 如果正在转场动画,取消手势 if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) { return NO; } // 判断手指移动方向 CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view]; if (translation.x <= 0) { return NO; } if (self.isCancle) { return NO; } return YES; } ``` 三、在NCPopGestureRecognizer的类实现中 在load方法中交换navVC的push方法和自定义的方法实例 ```objc + (void)load { Method originalMethod = class_getInstanceMethod([self class], @selector(pushViewController:animated:)); Method swizzledMethod = class_getInstanceMethod([self class], @selector(hm_pushViewController:animated:)); method_exchangeImplementations(originalMethod, swizzledMethod); } ``` 取消的方法调用上边自定义类的属性赋值 ```objc - (void)cancleHMFullScreenPopGesRec:(BOOL)cancle { [self hm_fullScreenPopGestureRecognizerDelegate].isCancle = cancle; } ``` 自定义push方法和手势 根据kvc取值拿到 target 和action添加到自定义手势 并禁用系统交换手势,设置手势代理为自定义类 ```objc - (void)hm_pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.popGestureRecognizer]) { [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.popGestureRecognizer]; NSArray *targets = [self.interactivePopGestureRecognizer valueForKey:@"targets"]; id internalTarget = [targets.firstObject valueForKey:@"target"]; SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:"); self.popGestureRecognizer.delegate = [self hm_fullScreenPopGestureRecognizerDelegate]; [self.popGestureRecognizer addTarget:internalTarget action:internalAction]; // 禁用系统的交互手势 self.interactivePopGestureRecognizer.enabled = NO; } if (![self.viewControllers containsObject:viewController]) { [self hm_pushViewController:viewController animated:animated]; } } ``` 用runtime给分类添加属性绑定 ```objc - (HMFullScreenPopGestureRecognizerDelegate *)hm_fullScreenPopGestureRecognizerDelegate { HMFullScreenPopGestureRecognizerDelegate *delegate = objc_getAssociatedObject(self, _cmd); if (!delegate) { delegate = [[HMFullScreenPopGestureRecognizerDelegate alloc] init]; delegate.navigationController = self; objc_setAssociatedObject(self, _cmd, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } return delegate; } - (UIPanGestureRecognizer *)popGestureRecognizer { UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd); if (panGestureRecognizer == nil) { panGestureRecognizer = [[UIPanGestureRecognizer alloc] init]; panGestureRecognizer.maximumNumberOfTouches = 1; objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } return panGestureRecognizer; } ```
本实例支付的费用只是购买源码的费用,如有疑问欢迎在文末留言交流,如需作者在线代码指导、定制等,在作者开启付费服务后,可以点击“购买服务”进行实时联系,请知悉,谢谢
感谢
0
手机上随时阅读、收藏该文章 ?请扫下方二维码
相似例子推荐
评论
作者
oragekk
9
例子数量
56
帮助
9
感谢
评分详细
可运行:
4.5
分
代码质量:
4.5
分
文章描述详细:
4.5
分
代码注释:
4.5
分
综合:
4.5
分
作者例子
ijkPlayer 集成
iOS 物流信息时间轴
ios图片轮播效果
自定义全屏返回手势
了解机器学习框架CoreML
微信开源组件WCDB漫谈及Demo
类支付宝微信密码输入框
iOS 神经网络模型训练
给flutter初学者的demo全集