Recent research on the iOS program sidebar. Gradually found iOS The program also began to take the wind sidebar that, QQ, today headlines, path (path operator's earliest side bar app that, the effect is said to be the path effect), so the study of the next.
Then you find that there are a lot of sidebar controls on the Git hub, and the controls are pretty good. But I want to find it. Do not use a third-party control to implement the sidebar? Later took part in this blog, and then made up their own, to figure out. Here's a concrete introduction.
1.
First we need to create a new 3 view Controlle inside the storyboard, which can also be navigation controller. But I still used to directly with the view controller to be able to jump all their own to achieve.
2.
Next, you need to create a new 3 class
Containerviewcontroller is a VC for a container class. The role is to place MAINVC and SIDEVC, just like Tabbarviewcontroller. It is just a container, the real adjustment of the page is in other VCs.
3.
Let's not worry about how these 3 Viewcontroller are implemented. We go to the storyboard. Set the identifier of 3 Viewcontroller respectively. Look like this.
Containerviewcontroller can not set storyboard, but MAINVC and SIDEVC must set the storyboard ID, and then you can edit the main VC and SIDEVC yourself. This allows you to see the effect of the sidebar more clearly.
Finally the storyboard is this:
The top is containerviewcontroller. Next from right to left are Mainviewcontroller and Sideviewcontroller respectively.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmte1njaxmg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
4.
Well, then we're going to start coding. The effect I want to do here is to slide the screen or click on the button in the top left corner of the MAINVC to open the sidebar and then, when the sidebar is displayed, swipe the screen or tap the MAINVC on the right side. Can hide the sidebar.
Let's take a step-by-step analysis of the code:
Actually, it's mainly containerviewcontroller.
ContainerViewController.h
This is the equivalent of a container VC, which contains the main interface and sidebar #import <UIKit/UIKit.h> #import "MainViewController.h" #import " SideViewController.h "@interface containerviewcontroller:uiviewcontroller<uigesturerecognizerdelegate>{}@ Property (Nonatomic, Strong) Mainviewcontroller *centercontroller; @property (nonatomic, strong) Sideviewcontroller * leftcontroller;-(void) showsideview;-(void) Hidesideview; @end
We import MAINVC and SIDEVC, then define two properties and two method
Containerviewcontroller.m
#import "ContainerViewController.h" @interface Containerviewcontroller () @end @implementation containerviewcontroller-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view. Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" bundle:nil]; _centercontroller = (Mainviewcontroller *) [Storyboard instantiateviewcontrollerwithidentifier:@ "MainViewController "]; _centercontroller.fatherviewcontroller = self; _leftcontroller = (Sideviewcontroller *) [Storyboard instantiateviewcontrollerwithidentifier:@ "Leftviewcontroller"] ; [Self.view Addsubview:_centercontroller.view]; [_centercontroller.view settag:1]; [_centercontroller.view SetFrame:self.view.bounds]; [Self.view Addsubview:_leftcontroller.view]; [_leftcontroller.view Settag:2]; [_leftcontroller.view SetFrame:self.view.bounds]; [Self.view Bringsubviewtofront:_centercontroller.view]; Add swipe gesture UiswipegesturerecognIzer *swipegestureright = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipegesture:)]; [Swipegestureright Setdirection:uiswipegesturerecognizerdirectionright]; [_centercontroller.view Addgesturerecognizer:swipegestureright]; Uiswipegesturerecognizer *swipegestureleft = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipegesture:)]; [Swipegestureleft Setdirection:uiswipegesturerecognizerdirectionleft]; [_centercontroller.view addgesturerecognizer:swipegestureleft];} -(void) Swipegesture: (Uiswipegesturerecognizer *) Swipegesturerecognizer {Calayer *layer = [_centercontroller.view Layer]; Layer.shadowcolor = [Uicolor blackcolor]. Cgcolor; Layer.shadowoffset = Cgsizemake (1, 1); layer.shadowopacity = 1; Layer.shadowradius = 20.0; if (swipegesturerecognizer.direction = = uiswipegesturerecognizerdirectionright) {[Self showsideview]; } if (swipegesturerecognizer.direction = = UiswipegesturerecOgnizerdirectionleft) {[Self hidesideview]; }}//Displays the sidebar. A function is written separately for MAINVC to call when the avatar is clicked-(void) showsideview{[_leftcontroller.view Sethidden:no]; [UIView Beginanimations:nil Context:nil]; [UIView Setanimationcurve:uiviewanimationcurveeasein]; if (_centercontroller.view.frame.origin.x = = Self.view.frame.origin.x | | _centercontroller.view.frame.origin.x = =- ) {[_centercontroller.view setframe:cgrectmake (_centercontroller.view.frame.origin.x+200, _CENTERCONTROLLER.VI EW.FRAME.ORIGIN.Y, _centercontroller.view.frame.size.width, _centercontroller.view.frame.size.height)]; } [UIView commitanimations];} -(void) hidesideview{[UIView Beginanimations:nil Context:nil]; [UIView Setanimationcurve:uiviewanimationcurveeaseout]; if (_centercontroller.view.frame.origin.x = =) {[_centercontroller.view setframe:cgrectmake (_centerController. view.frame.origin.x-200, _CENTERCONTROLLER.VIEW.FRAME.ORIGIN.Y, _centercontroller.view.frame.size.width, _cenTerController.view.frame.size.height)]; } [UIView commitanimations]; Last call to prevent white bottom [_leftcontroller.view sethidden:yes];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end
In the Viewdidload method, we get two viewcontroller from storyboard, notice that my sideviewcontroller name is Leftviewcontroller, That is, in the storyboard ID to write this name.
And then joined the swipe gesture, each left and right slide
The action of sliding screen is defined in the agent of slide screen. Why do you make the show/hide Sideview alone in two method? Because we want to be real now MAINVC inside the click on the avatar can be called CONTAINERVC in the two functions!
Now, let's see how MAINVC can be achieved.
MainViewController.h
#import <UIKit/UIKit.h> @class containerviewcontroller; @interface mainviewcontroller:uiviewcontroller-( ibaction) Showsideview: (ID) sender; @property (nonatomic, strong) containerviewcontroller* Fatherviewcontroller; @end
Note here that @class is used to introduce CONTAINERVC, not #import in the header file to prevent circular references.
Then we define a property. Fatherviewcontroller, it is an example of a containerviewcontroller.
Showsideview's ibaction is the click action of the Avatar button.
Mainviewcontroller.m
#import " MainViewController.h "#import" ContainerViewController.h "@interface Mainviewcontroller () @end @implementation mainviewcontroller-(void) viewdidload {[Super viewdidload]; Do any additional setup after loading the view.} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */-(ibaction) Showsideview: (ID) Sender {[Self.fatherviewcontroller showsideview];} -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[Self.fatherviewcontroller Hidesideview];} @end
So in the Mainviewcontroller click on the Avatar button action can call Fatherviewcontroller, that is, Containerviewcontroller inside the Showsideview action.
Touchesbegan is when you click Mainviewcontroller. Hides the sidebar.
Think Sideviewcontroller is in the storyboard inside drag into the control finished. So there's no need to write any code.
Of course, there's a sidebar on the left side, and you want to look at the sidebar method on both sides. Check here.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
iOS develop no third-party controls to reach the effect of the side bar