iOS configure global swipe back gesture

In fact, if you use the system's back button and do not customize it with leftBarButtonItem or leftBarButtonItems, then the system will have its own sliding back function. However, in actual development, the back button will be customized according to your own needs. If you customize it in the above two ways, or add a scroll view such as UIScrollView to the current view, the gesture of sliding back will automatically invalid. Because this will overwrite the original proxy of the sliding return gesture, so my solution is to re-add the proxy to UINavigationController

1 Create a class that inherits from UINavigationController as a global navigation controller, and then add a side sliding proxy inside

- (void)viewDidLoad {
    [super viewDidLoad];
    // 重新设置侧滑手势的代理
    __weak typeof(self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakSelf;
    }
}

2 After the setting is completed, all ViewControllers under the navigation controller have the function of sliding back, but if the root controller is sliding, there will be problems. When I want to push the new one after the root controller is sliding controller will fail. If the root controller slides sideways three times, it needs to be pushed four times before a new view can appear, so the side slide gesture of the root controller needs to be processed through the proxy method of the gesture

// 开始接收到手势的代理方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // 判断是否是侧滑相关的手势
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        // 如果当前展示的控制器是根控制器就不让其响应
        if (self.viewControllers.count < 2 ||
            self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    return YES;
}

3 At this point, it can be said that most of the global side slide return gestures have been completed, but if you add a scroll view such as UIScrollView to the current view, you will find that the side slide function has disappeared, because the scroll view can receive and respond to the side slide gesture. The navigation control will not be passed, so if you want to have the function of sliding back in the controller with the scroll view, you must process the multi-gesture event.

// 接收到多个手势的代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    // 判断是否是侧滑相关手势
    if (gestureRecognizer == self.interactivePopGestureRecognizer && [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint point = [pan translationInView:self.view];
        // 如果是侧滑相关的手势,并且手势的方向是侧滑的方向就让多个手势共存
        if (point.x > 0) {
            return YES;
        }
    }
    return NO;
}

Remark:

Call this method to disable the sliding back function

[self.navigationController.interactivePopGestureRecognizer setEnabled:NO];

This method will disable the side swipe gesture of all ViewControllers behind the navigationController, so I recommend enabling the global swipe function in viewDidLoad of all ViewController base classes, and then prohibiting swiping in viewDidAppear in the corresponding controller that prohibits swiping.


Remember to disable it in viewDidAppear. If you disable it in viewWillAppear, when you push to the next interface and then use side slide to return, you will first go to the viewWillAppear of a controller to disable its side slide, resulting in the next interface will also be Lose the side-slip function
If you can't understand it, look at the life cycle of the controller


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325976598&siteId=291194637