iOS7滑动返回

来源:互联网 发布:淘宝评价引流软件下载 编辑:程序博客网 时间:2024/04/30 03:56

原文链接:http://blog.csdn.net/u011545443/article/details/17090809

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 working5     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官方文档中对其的描述很少,如下:

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的背景的一种实现方式:

 

今天看到了作者的一篇说明帖,深感之前的思考太过肤浅,此处将iOS6下滑动返回的实现独立出来:

http://www.cnblogs.com/lexingyu/p/3434340.html

以上来源:http://www.cnblogs.com/lexingyu/p/3432444.html


在iOS 7 对UINavigationController提供了左侧边缘右滑返回的功能,但是在自定义了leftBarButtonItem之后,该功能就消失了,经过在我们的google之后,发现需要添加这么一句:

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
问题就迎刃而解了。

来源:http://blog.csdn.net/mayixingkong1/article/details/13016091


响应了iOS7的滑动返回手势之后,我们还会发现一个问题,iOS7的滑动返回不会调用我们给leftBarButtonItem设置的回调,那么返回的事件如何捕捉呢?可以通过以下代码:
 
[plain]
 
- (void)viewWillDisappear: (BOOL)animated  
{  
    [super viewWillDisappear: animated];  
    if (![[self.navigationController viewControllers] containsObject: self])  
    {  
        // the view has been removed from the navigation stack, back is probably the cause  
        // this will be slow with a large stack however.  
    }  
}  
来源:http://www.2cto.com/kf/201311/257635.html

还没有使用过,不知道效果怎么样。。仅供参考!

0 0
原创粉丝点击