iOS 7的手势滑动返回
如今使用默认模板创建的iOS App都支持手势返回功能,假设导航栏的返回button是自己定义的那么则会失效,也能够參考这里手动设置无效。
- if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
- self.navigationController.interactivePopGestureRecognizer.enabled = NO;
- }
假设是由于自己定义导航button而导致手势返回失效,那么能够在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的手势滑动返回功能
本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147 之前随手写过一篇<使用UIScreenEdgePanGestureR ...
- 禁用ios7 手势滑动返回功能
禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...
- Android-通过SlidingMenu高仿微信6.2最新版手势滑动返回(二)
转载请标明出处: http://blog.csdn.net/hanhailong726188/article/details/46453627 本文出自:[海龙的博客] 一.概述 在上一篇博文中,博文 ...
- Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)
近期更新了微信版本号到6.2.发现里面有个很好的体验,就是在第二个页面Activity能手势向右滑动返回,在手势滑动的过程中能看到第一个页面,这样的体验很赞,这里高仿了一下. 这里使用的是v4包里面的 ...
- iOS之手势滑动返回功能-b
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- iOS之手势滑动返回功能
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- iOS 应用全部添加滑动返回
if ([self class] == [HomeViewController class]||[self class] == [ComprehensivefinanceViewControlle ...
- ios 侧边手势滑动返回 禁用/开启 功能
// 禁用 返回手势 if ([self.navigationController respondsToSelector:@selector(interactivePopGestureR ...
- iOS开发解决页面滑动返回跟scrollView左右划冲突
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithG ...
随机推荐
- 【LeetCode】To Lower Case(转换成小写字母)
这道题是LeetCode里的第709道题. 题目要求: 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串. 示例 1: ...
- pytorch将cpu训练好的模型参数load到gpu上,或者gpu->cpu上
假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net() 1. cpu -> cpu或者gpu -> ...
- z作业二总结
这是我的第二次作业,之前在课上所学的我发现已经忘得差不多了,这次的作业让我做的非常累,感觉整个人生都不太好了. 作业中的知识点:int(整型) float(单精度) double(双精度) char( ...
- 【JavaScript 13—应用总结】:锁屏遮罩
导读:上次说了,当弹出登录框时,由于背景色和弹出框时一样的,这样子,其实比较难聚焦到底该操作哪一块.所以,如果,有了颜色的区分,那么通过屏幕遮罩的效果,就可以将我们希望要被处理的东西突出显示.也就达到 ...
- BZOJ 1113 Wall ——计算几何
凸包第一题. 自己认为自己写的是Andrew 其实就是xjb写出来居然过掉了测试. 刚开始把pi定义成了int,调了半天 #include <map> #include <cmath ...
- SPOJ GSS6 Can you answer these queries VI ——Splay
[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...
- 转:sudo 的常见用法和参数选项
原文链接:http://wiki.ubuntu.org.cn/Sudo sudo,以其他用户身份执行一个命令. 用法 sudo -h | -K | -V sudo -v [-Akns] [-g gro ...
- 关于parseDouble用法
1.JAVA中的compareTo方法和strcmp完全类似,你可以使用 if(greeting.compareTo("help")==0).....或者用s.quals(t)来判 ...
- Python爬虫--beautifulsoup 4 用法
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构, 每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSo ...
- ftrace的使用
This article explains how to set up ftrace and be able to understand how to trace functions. It shou ...