Read the source of the Residemenu

Source: Internet
Author: User

Residemenu is a well-known open source repository on GitHub, mainly for the slide-by menu, with more than 3,000 star now. The effect is as follows.


It is said that the idea comes from a design of dribbble, or rather good-looking. Interested can go to GitHub on the search Residemenu, address is not affixed, choose this open Source Library The main reason is to take everyone to learn how to create a custom Viewcontroller container is the steps.
In fact, the view container everyone in use every day, what Navigationcontroller,tabbarcontroller,pageviewcontroller, may be the third people less use, System album click Browse Large Map, Then swipe left and right to see the next graph of the page is Pageviewcontroller, more like Tableviewcontroller, is also data-driven.
Why use the View container to believe also do not need me to repeat, a lot of benefits, imagine if navigationcontroller in the stack is not viewcontroller but a view, estimated that your main view class code to more than 10,000 lines.
As we all know, push and pop in Navigationcontroller.


Although the picture is ugly, but probably the meaning believes everybody is clear. But have you ever thought that every time you execute

[self.navigationController pushViewController:vc1 animated:YES];

What did the system do at the time of this sentence? In fact, this is what the system does.

 [self addChildViewController:vc1];                 // 1 vc1.view.frame = [self frameForContentController]; // 2 [self.view addSubview:vc1.view]; [vc1 didMoveToParentViewController:self]; // 3

Self Here you can think of as Navigationcontroller.
In the first step, Navigationcontroller uses the Addchildviewcontroller function to add VC1 as its own child view controller.
The second step is to set VC1 this view controller to the Reagan view frame.
The third step is to add the Vc1.view to the Navigationcontroller.view.
Fourth step, with Didmovetoparentviewcontroller This method notification has been push completed.
After the push, let's talk about the operation of pop. This is actually what this is all about. Let's say you've alloc and init before you've finished this viewcontroller called VC1.

[vc1 willMoveToParentViewController:nil];  // 1[vc1.view removeFromSuperview];            // 2[vc1 removeFromParentViewController];

The first step is to use the Willmovetoparentviewcontroller method and set the parameter to nil, notifying VC1 that the parent view controller is about to be removed.
The second step is to remove the Vc1.view from the parent view controller's view.
The third step, and then use Removefromparentviewcontroller this method to completely remove the VC1.
(This is my repeated emphasis on the routine, the meaning of the routine is that you only need to know it, do not understand why it is OK.) Because that's how people design the API. You don't have to ask why it's so designed. )
The Official document address is here. https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ Creatingcustomcontainerviewcontrollers/creatingcustomcontainerviewcontrollers.html#//apple_ref/doc/uid/ Tp40007457-ch18-sw6

When I saw this, I thought I understood, in fact, I do not understand.
There are a few questions.
1. The parent container can only add one when the child view controller is added, if the second sub-view controller must be removed before the first one.
Answer: Nonsense, of course, not only add one, you push->push->push when clearly addchildviewcontroller three back, and then you pop->pop-> Pop will only perform removefromparentviewcontroller. was also executed 3 back.

2. If the parent view container adds 3 sub-view containers, can the view of several sub-view containers be displayed at the same time?
Answer: Of course not, if you like, you can show 100 at the same time. Because frame is what you set yourself. How many settings you want to set.

OK, I believe you. If you understand the above two questions, you should understand what the so-called Parent view controller is all about.

Let's look at the source code of Residemenu.

RESideMenu *sideMenuViewController = [[RESideMenu alloc] initWithContentViewController:navigationController                                                                leftMenuViewController:leftMenuViewController                                                               rightMenuViewController:rightMenuViewController];

This is a Residemenu initialization method, in fact, the method name is very easy to understand, set a content Viewcontroller, this content Viewcontroller is a navigationcontroller, Then add a left menu Viewcontroller, and a right menu viewcontroller.
No one guessed, what exactly did this function do?
Very simple, executed three times this thing Bai.

 [self addChildViewController:content];                 // 1 content.view.frame = [self frameForContentController]; // 2 [self.view addSubview:self.currentClientView]; [content didMoveToParentViewController:self];

That content is our left menu view, right menu view and content view, respectively.
Don't believe us to see his source.


Hey yo, I'm going, it's embarrassing, not executing the above code, just the assignment operation, is the Residemenu class has three member variables, respectively, is

Hey, I'm not superstitious yet. Keep looking down. See Viewdidload () this method.

I know my judgment is not wrong, still have this method. That is to say that when the residemenu loaded into the viewdidload, judge left and right is not present, if not for nil let Residemenu put these two controller plus content Controller is added as Childviewcontroller.

Note that this column adds the order of the Child View Controller view, plus the left view, plus the right view, plus the content view. Then you should have a picture in mind, is the content of the view at the top, because people enter the app must be the first to see the contents of the view, I can not go inside let the user see the menu view right, You need to let the user use gestures to pull up or click on the top left corner of the button (usually three bar of the kind of button, commonly known as Burger button) This time the menu view will be displayed. (This is generally the case, do not rule out some of the wonderful app to show people the menu.) )

So the play is coming, how to call the left menu, then the content view does not completely disappear, but only a part of the leak.


And then we found this section in the source code.


In fact, it is good to understand, in order to display the left menu view, the view of the hidden set to No, the right view of the hidden set to Yes, it is estimated that the previous set to Yes, here to let the left view display. Then you set the contents of the menu on the left.
And then add the animation, to Contentviewcontroller's view made a scale, it is estimated that the Contentviewcontroller view compressed a bit, Then after the compression change Contentviewcontainer Center, hey wait a moment, how to have a contentviewcontainer?
I looked forward again, oh, the original residemenu has two member variables are uiview type, a view to put the menu sub-view, a view to put the content views.
Then we'll comb the process.
1. When the user clicks the Hamburger button to display the left menu, first set the hidden of the left view to No. This shows the left view.
2. In order to have the effect of the first picture, the view of the content to make a transform in the scale,xy compressed smaller, and then change the content view of the center, let him on the right.
This effect is then formed.

Then the right menu should be the same process.

Then the basic process understand, in fact, the rest is how to beautify these processes.

To give you a physical problem, the general side-slide Menu This thing is to click on the menu to switch the content view, then switch the content view when the method used?

Finally, someone may be curious about how residemenu does it as the finger slides to the right and then the animation slowly executes the process.
So take a closer look.

- (void)panGestureRecognized:(UIPanGestureRecognizer *)recognizer

 

Read the source of the Residemenu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.