Slide back to the previous level

  1. slide back

    • Since iOS7.0, NavigationController has added a sliding back function. When a controller is pushed and the View of the controller is displayed, in addition to clicking the back button at the top left, you can also slide the left edge of the screen to the right. Achieve the effect of returning to the previous interface
    • But the problem with sliding back is that we often use a custom back button in our projects. When you use a custom button, you will find that the sliding back effect hangs up
  2. Principle Analysis of Side Slip Return

    1. gesture:

      • To slide back, you need to slide your finger on the screen, so it can be inferred from this point that the slide back function should be a UIGestureRecognizer

      • Enter the header file of UINavigationController, search for UIGestureRecognizer , then we will find an attribute, which is the gesture used to manage the sliding back function of the navigation controller

          // 这就是侧滑返回手势
          @property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) 
        
      • By looking at the documentation, you can see that this gesture is specifically used to perform the pop function and remove the top controller of the stack. At the same time, you can also add other gestures to the current View and bind it with the slide back gesture ( but try to Don't use it like this, don't you think it's anti-human... )

    2. acting:

      • This gesture is readonly, which means you can't modify it, but it really fails when you customize the button. The real reason for this can be reminiscent of the proxy method in the UIGestureRecognizerDelegate proxy

          // 这个方法返回的BOOL值, 决定这个手势是否能够生效
          - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
        
      • From this point, it can basically be concluded that the proxy of the side swipe return detects that you have customized the back button, so it intercepts the side swipe return gesture

      • And all we have to do is not let the proxy intercept him

  3. Resume the swipe back gesture

    1. First, in the custom NavigationController, when the ViewDidLoad method is called, set a proxy for the side slide return gesture. Note that the current class must comply with the <UIGestureRecognizerDelegate>protocol

       self.interactivePopGestureRecognizer.delegate = self;
      
    2. Manually implement proxy methods to make gestures available

      • It must be noted here: if you do not judge the number of current sub-controllers and return YES directly, then your root controller will also respond to the side slide return gesture
      • If the root controller uses side slide to return, your UI interface will become suspended animation , this is a very serious bug, so be careful
        • (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
          return self.childViewControllers.count > 1;
          }



Author: Batter
Link : https://www.jianshu.com/p/4e4edad6f48e
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

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