I wrote an article about the smooth transition of the navigation bar before, the main function is to achieve smooth transition effect for the navigation bar in the process of controller transition. Back to the controller transition itself, it also has the following pain points:

  • You can swipe left or right to push with your hands.

  • A pop in the opposite direction can be performed with a gesture.

  • Can control the transition function flexibly.

## Source code structure based on the above points to implement a simple open source framework: XANavBarTransition. On the basis of smooth transition of the original navigation bar, some transition functions are added and the intrusion of the program is reduced to the greatest extent. Here is a brief discussion of the design ideas.

Design ideas

At the beginning of the design, I wanted to control the transition of the navigation controller in a global way, but later found that this way could not allow me to manage the transition scheduling between controllers. This is also a difficult problem I encountered when working on the XANavBarTransition framework. The final decision: The interactive transitions of each controller in the navigation controller stack and smooth transitions of the navigation bar are regarded as independent transitions (the controller manages the creation and destruction of transition sessions), while the navigation controller still controls and manages global properties and methods.

The transition scheme mentioned above is that when the controller is created, the user has to choose whether to push to the left or to the right. Either way, the opposite direction is pop. This is true of all controllers in the controller stack. Also determine the customization functions, such as: A page I want push, a page I want pop, and so on.

Both push and POP operations are managed by an internal concrete transition object, which is created and destroyed as the controller displays it. So it is subclassed each time it is created based on the transition scheme.

At the beginning, I wanted to complete it through the pop function of the system, but later I found that the control was insufficient. The specific implementation idea of push and POP is actually very simple: gesture is added to the controller, push or POP is determined according to the transition scheme and sliding direction, and corresponding interactive transition animation is driven.

Here is a code for gesture recognition:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
    if(gestureRecognizer == self.interactivePan){
        CGPoint point     = [gestureRecognizer translationInView:nil];
        CGPoint velocity  = [gestureRecognizer velocityInView:nil];
        if (fabs(velocity.y) > fabs(velocity.x)) {// The vertical direction is not processed
            return NO;
        }
        if(self.nc.xa_isTransitioning){
            return NO;
        }
        if([self getPushCondition:point]){//push
            self.nextVC = [self.transitionDelegate xa_nextViewControllerInTransitionMode:self.transitionMode];// Is a valid push controller
            if(self.nextVC  == nil| | [self.nc.childViewControllers containsObject:self.nextVC]){
                return NO;
            }
            return self.pushTransitionEnable;
        }else if([self getPopCondition:point]){//pop
            if(self.nc.viewControllers.count <= 1) {// Stack bottom controller does not handle
                return NO;
            }
            return self.popTransitionEnable;
        }
        return NO;
    }
    return YES;
}
Copy the code

State control

The framework of XANavBarTransition mainly has the following control states.

  • Each controller has the ability to pop, which is done in the gesture recognition phase.

  • Can control the transition function of all controllers under the current navigation controller. This is done during the transition session creation and property setting phase.

  • Agent callback: XANavigationControllerObserver object is responsible for monitoring the < UINavigationControllerDelegate > proxy method. If there is a need to monitor App external, can realize the < UINavigationControllerDelegate > protocol proxy Settings to the navigation controller xa_delegate attributes, internal transfer will do.

At the end

If you have any questions in the use of the framework, please feel free to send me issues or @ about ARashi.