iOS development does not use third-party controls for sidebar effects

Source: Internet
Author: User

Recently in the study of iOS program side bar, because found that gradually iOS program also began to walk the style of the sidebar, QQ, today's headlines, path (path calculates the first sidebar of the app, so also the side bar effect is said to be the Path effect), so the study of the next.

And then I found that there are a lot of sidebar controls on the Git hub, and the controls are pretty good, but I want to find a way to implement the sidebar without a third-party control. Later, referring to this blog, and then made their own, to figure out. Let me introduce you in detail below.

1.

First of all we need to create a new 3 view Controlle in storyboard, here can also be navigation controller, but I am accustomed to directly with the view controller can be, jump all their own to achieve.

2.

Next you need to create a new 3 class,

Containerviewcontroller is a container class VC, the role is to place MAINVC and SIDEVC, just like Tabbarviewcontroller, it is just a container, really adjust the page is in other VC.

3.

Do not care about how these 3 viewcontroller, we go to storyboard, respectively, set 3 Viewcontroller identifier, like this


Containerviewcontroller can not set storyboard, but MAINVC and SIDEVC must set the storyboard ID, then you can also edit the main VC and SIDEVC, This allows you to see the effect of the sidebar more clearly.

The final storyboard is this:

The top is Containerviewcontroller, and the next from right to left is Mainviewcontroller and Sideviewcontroller.


4.

Well, then we'll start coding. The effect I want to do here is to slide the screen or click on the MAINVC button in the upper left corner to open the sidebar, and then when the sidebar is displayed, slide the screen or click on the right side of the MAINVC to 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, which is written separately as a function for MAINVC to call when clicking on the Avatar-(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.

Then I added the swipe gesture, which is slide to the left and slide to the right.

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 are going to be able to call these two functions in the CONTAINERVC when I click on the avatar in MAINVC!

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 that this is used @class to introduce CONTAINERVC, not in the header file #import is to prevent circular references.

Then we define a property,fatherviewcontroller, which 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, Hide Sidebar.

Think that Sideviewcontroller is dragged into the storyboard inside the control, so there is no need to write any code.

Of course, here is just the sidebar on the left side, want to look at the side sidebar method, check here.



iOS development does not use third-party controls for sidebar effects

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.