IOS realization of the right slide pop function

Sliding return results


Renderings


IOS7 from the start, the system was UINavigationControlleradded interactivePopGestureRecognizerattribute, which is the right of return slip system provides animation, we can achieve by sliding to the left edge popeffect. We can ,如上图所示。self.navigationController.interactivePopGestureRecognizer.enabled = NOreturn to the animation shut down the system default edge.

Disadvantages:

  • Dragging the edge can only be achieved, if the portion of the view from the left side than the other edge is not effective.
  • If pushthe other interface, if rewriting leftBarButtonItembecomes ineffective.

If you solve two problems above, we need to achieve animation effects. The system only provides edge gesture, without providing any animation in the right place can slip return, so this need of our own to create a UIPanGestureRecognizergesture. Article will explain in detail how to achieve later.
So, there is a problem, that is, if the rewrite leftBarButtonItemedge gesture system becomes ineffective, this how to solve it? In fact, very simple, we only need to rewrite leftBarButtonItemadd the following code to the interface:

self.navigationController.interactivePopGestureRecognizer.delegate = self;
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //导航的rootViewController关闭右滑返回功能
    if (self.navigationController.viewControllers.count <= 1)
    {
        return NO;
    }

    return YES;
}

Can be seen that the above code: reset the interactivePopGestureRecognizerproxy object, interactivePopGestureRecognizeractually UIGestureRecognizera subclass, and then override gestureRecognizerShouldBegin:the method determines the root function of the navigation controller does not need to return the slip. Download github.

prompt:

 self.navigationItem.leftBarButtonItem = customLeftBarButtonItem; //右滑返回失效
 self.navigationItem.backBarButtonItem = customLeftBarButtonItem; //不影响右滑返回

So, if you implement a custom slide the right to return it? The code is simple.
1. Custom a parent class for all controllers, such as DLBaseViewController,
then DLBaseViewControllerthe viewDidLoadmethod of adding the code below:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //1.获取系统interactivePopGestureRecognizer对象的target对象
    id target = self.navigationController.interactivePopGestureRecognizer.delegate;
    //2.创建滑动手势,taregt设置interactivePopGestureRecognizer的target,所以当界面滑动的时候就会自动调用target的action方法。
    //handleNavigationTransition是私有类_UINavigationInteractiveTransition的方法,系统主要在这个方法里面实现动画的。
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
    [pan addTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];
    //3.设置代理
    pan.delegate = self;
    //4.添加到导航控制器的视图上
    [self.navigationController.view addGestureRecognizer:pan];

    //5.禁用系统的滑动手势
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

#pragma mark - 滑动开始会触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //只有导航的根控制器不需要右滑的返回的功能。
    if (self.navigationController.viewControllers.count <= 1)
    {

        return NO;
    }

    return YES;
}

Below all of our classes are inherited DLBaseViewControllercan be. Anywhere sliding interface can slip the right to return. FIG operation effect as follows. Demo Download


Renderings

2. If TabbarController, the above method also can be used? We create a TabbarController project, then the test results are as follows:


Based TabbarController

Note: The above TabBar into the hidden page 2, is selected in the storyboard TwoViewControlleron the hook Hide Bottom Bar on Push
as shown below:


Page 2 into the hidden tabbar

3. The line of code to get the right slide return function.
Above we wrote the function code inside the parent class, so the downside is that each controller must have inherited the same parent class, so much trouble. And above code needs to be performed once for each controller, the efficiency is relatively low. Based on the above disadvantages, and then runtimethe method swizzlingpackage a more simple way powerful technology, one line of code to get. Detailed demo

/*
  开启右滑返回手势
  注意:
  1.一定要在appDelegate里面开启,并且在UINavigationController初始化之前,否则没有效果。
  2.如果用的storyboard,直接在appDelegate类didFinishLaunchingWithOptions开启即可
 */
 [DLNavigationTransition enableNavigationTransitionWithPanGestureBack];

Note: Because by runtimein UINavigationControllerthe viewDidLoadadded gesture code inside, so be sure to appDelegateturn on the inside, and UINavigationControllerbefore the initialization, otherwise no effect. If the project is used storyboarddirectly in the appDelegateclass didFinishLaunchingWithOptionsturned to.




Guess you like

Origin blog.csdn.net/qq_26918391/article/details/76828133