Ios_19_ Drawer Effect _ imitation NetEase

Source: Internet
Author: User

Eventually:



Mainstoryboard:



BeyondViewController.h

  beyondviewcontroller.h//  19_ Drawer effect _ imitation netease////  Created by Beyond on 14-8-1.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> #import "LeftTableViewControllerDelegate.h" @ Interface beyondviewcontroller:uiviewcontroller//left half (display column list) @property (weak, nonatomic) Iboutlet UIView *leftview;  Right half (display personal information settings view) @property (weak, nonatomic) Iboutlet UIView *rightview;//Topmost, the largest full screen is the front view @property (weak, nonatomic) Iboutlet UIView *mainview;//title button in the title Status bar view above (NetEase logo image and column name horizontal arrangement) @property (weak, nonatomic) Iboutlet UIButton *titleb The lower half of the tn;//Mainview  is the  text view, showing the sub-columns of the View@property (weak, nonatomic) Iboutlet UIView *contentview;//pan-pull gesture processing- (ibaction) Pangesture: (Uipangesturerecognizer *) sender;//Mainview The upper part of the title status bar view left and right button-(ibaction) Btnclick: ( UIButton *) sender; @end



Beyondviewcontroller.m

beyondviewcontroller.m//19_ Drawer Effect _ imitation NetEase////Created by Beyond on 14-8-1.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" #import "LeftTableViewController.h" #import " RightViewController.h "#import" Column.h "#import <quartzcore/quartzcore.h>//gesture at the end of X#define kendx frame.origin.x//left View width # define Kleftwidth _leftview.frame.size.width//Right View width # define Krightwidth _ rightview.frame.size.width//Advance declaration of the agreement @protocol Lefttableviewcontrollerdelegate; @interface Beyondviewcontroller ()        <lefttableviewcontrollerdelegate>{//Finger press the time, remember, mainview the beginning x cgfloat _startx;    Member variables, remember the instance of the left controller Lefttableviewcontroller *_LEFTVC;            Member variables, remember the instance of the right controller Rightviewcontroller *_RIGHTVC;    Dictionary, remember all the sub-controllers that instantiate the column, avoid recreating nsmutabledictionary *_columnviewcontrollers every time; } @end @implementation beyondviewcontroller//hide the status bar-(BOOL) prefersstatusbarhidden{return YES;}        -(void) viewdidload{[Super Viewdidload]; _titlebtn.backGroundcolor = [Uicolor Clearcolor];        0 dictionaries, remember all the sub-controllers that instantiate the column, avoid recreating _columnviewcontrollers = [Nsmutabledictionary dictionary] every time; 0, set the navigation bar background for NetEase Red//[[uinavigationbar appearance] setbackgroundimage:[uiimage imagenamed:@ "Bg.png"] Forbarmetri    Cs:uibarmetricsdefault];        The status bar color changes to the default style//[uiapplication Sharedapplication].statusbarstyle = Uistatusbarstyledefault;    1, add the view of the left controller to the left view inside _LEFTVC = [[Lefttableviewcontroller alloc]init]; Key code in order to get to the left side of the controller is clicked, the corresponding column data model object, the main controller becomes the agent of the left controller, adhere to its fixed protocol, the implementation of the Protocol method, so that the left controller is clicked line number corresponding data model object _    Leftvc.delegate = self;        _leftvc.view.frame = Self.leftView.bounds;        [Self.leftview Addsubview:_leftvc.view];        2, similarly, add the view of the right controller to the right view inside _RIGHTVC = [[Rightviewcontroller alloc]init];        _rightvc.view.frame = Self.rightView.bounds;    [Self.rightview Addsubview:_rightvc.view];        3, the first time loading, it should display the news sub-column controller to the navigation controller, and then add the navigation controller view to Mainview inside [self firstloading]; }//custom method, sectionOnce loaded, you should display the controller of the news sub-column to the navigation controller, and then add the navigation controller view to Mainview inside-(void) firstloading{column * column = [column columnnamed:@ "New        Smell "imgname:@" news.png "classname:@" Newsviewcontroller "]; Only need to call a Leftviewcontroller proxy method manually, lefttableviewrowclicked, pass a news sub-column can [self lefttableviewrowclicked:column];} Pan drag gesture Processing-(ibaction) Pangesture: (Uipangesturerecognizer *) sender{//If it is just pressed, remember that Mainview's start x if (Uigestur    Erecognizerstatebegan = = sender.state) {_startx = self.mainview.frame.origin.x;        }//Pan drag distance cgpoint delta = [Sender Translationinview:_mainview];        CGRect frame = self.mainView.frame;        Calculate new X-values and make robustness judgments kendx = _startx + delta.x;    1, limit the maximum drag range if (kendx >= kleftwidth) {kendx = Kleftwidth;    } if (Kendx <=-krightwidth) {kendx =-krightwidth;        }//2, because left view and right view overlap, so hide one of the if (Kendx > 0) {//NSLog (@ "-call frequency is quite high--");        _rightview.hidden = YES; _leftview.hidden = NO;        } else {_rightview.hidden = NO;    _leftview.hidden = YES; } if (uigesturerecognizerstateended = = sender.state) {//gesture ends when robust judgment is required//2  , analysis end let go of the position x, decide to expand to what extent *//2.1 If you drag only a little to the right, less than 1/2 of the width of the left view, then 0 if (Kendx < 0.5*kleftwidth &&        Kendx >= 0) {kendx = 0; }else if (kendx >= 0.5*kleftwidth && kendx <= kleftwidth) {//2.2 If you drag the more than half to the right, the width of the left view is greater than 1/2, although        Not yet in place, can also be considered to be in place kendx = Kleftwidth;            }else if (Kendx >-0.5*krightwidth && kendx <= 0) {//2.3 If you drag only a little to the left and the width of the right view is less than 1/2, then 0        kendx = 0;  }else if (kendx <=-0.5*krightwidth) {///2.4 If left-drag more than half, the width of the right view is greater than 1/2, although not in place, it can be considered in place kendx        =-Krightwidth;             }*///2nd Way of judging//starting with 0,delta.x greater than 0 for the right swipe if (_startx = = 0 && delta.x >0) { Kendx = Kleftwidth;        }else if (_startx = = 0 && delta.x < 0) {//start with 0,delta.x less than 0 for left swipe kendx =-Krigh        Twidth;  }else if (_startx = = Kleftwidth && delta.x < 0) {//start with kleftwidth,delta.x less than 0 for left swipe Kendx        = 0;            }else if (_startx = =-Krightwidth && delta.x > 0) {//start with-krightwidth,delta.x greater than 0 means swipe right        kendx = 0; }}//Finally, set Mainview's new frame [UIView animatewithduration:0.2 animations:^{Sel    F.mainview.frame=frame;                }];    Finally, add a shadow effect to the layer where the Mainview is located [self addshadowformainviewwithendx:kendx]; }//custom method to add a shadow effect to the layer where Mainview is located (very high call frequency)-(void) Addshadowformainviewwithendx: (cgfloat) endx{//1, click Project, plus, import 3rd party Framework #    Import <QuartzCore/QuartzCore.h>//2, get the layer where the Mainview is located, set the shadow Parameter//NSLog (@ "Call frequency is very high---"); _mainview.layer.shadowcolor = [Uicolor blackcolor].    Cgcolor;    _mainview.layer.shadowopacity = 0.5; if (endx >= 0) {_mainview.layer.shadowoffset = Cgsizemake (-5, 0);    } else {_mainview.layer.shadowoffset = Cgsizemake (5, 0); }//Click the button, you can expand the left sidebar-(ibaction) Btnclick: (UIButton *) sender{//define a temporary variable cgfloat StartX = _mainview.frame.ori            gin.x;            Add a shadow effect to the layer where Mainview is located [self AddShadowFormainViewWithEndX:sender.tag = = 1?1:-1];    Defines a temporary variable cgfloat tempendx = 0;        The left button is clicked if (1 = = Sender.tag) {//Hide right half _leftview.hidden = NO;                _rightview.hidden = YES;        if (StartX = = 0) {tempendx = Kleftwidth;        }else if (StartX = = kleftwidth) {tempendx = 0;        }} else {//Click the right button to hide the left half _leftview.hidden = YES;        _rightview.hidden = NO;        if (StartX = = 0) {tempendx =-krightwidth;        }else if (StartX = =-Krightwidth) {tempendx = 0;  }}//finally set Mainview X, invoke the extracted public code, set Mainview x, parameter is endx [self setmainviewx:tempendx];      }//extracted public Code, set MainView x, parameter is endx-(void) Setmainviewx: (cgfloat) endx{cgrect frame = self.mainView.frame;    frame.origin.x = EndX;    [UIView animatewithduration:0.2 animations:^{self.mainview.frame=frame;    }]; }//the most critical method, the left side of the controller's proxy method, the current left side of the controller will be called when a row is clicked-(void) lefttableviewrowclicked: (ID) columnselected{Column *column = (Col    Umn *) columnselected;                1, close the left control =======================//Call the extracted public code, set Mainview x, parameter is endx [self setmainviewx:0];        2, change the caption button above the text _titlebtn.titlelabel.text = Column.columnname; According to the class name in the column data model, the controller of the corresponding column is instantiated, and it is set as the root controller of the navigation controller, and finally the view of the navigation controller is added to the Mainview in order to conveniently set the navigation bar, as well as the jump//2 of each controller, from the cache dictionary, if the sub-control    The device dictionary has a sub-controller that has ever been created and is directly removed to use uiviewcontroller *COLUMNVC = _columnviewcontrollers[column.columnclassname];        If the controller in the sub-controller dictionary is not saved, the sub-controller is created if (COLUMNVC = = nil) {Class c = nsclassfromstring (Column.columnclassname);        COLUMNVC = [[C Alloc]init]; And be sure to put it in the sub-controller dictionary and save it [_columNviewcontrollers SETOBJECT:COLUMNVC ForKey:column.columnClassName]; }//4, remove the old child view if (_contentview.subviews.count > 0) that is being displayed in Contentview {UIView *oldview = [_cont        Entview Subviews][0];    [Oldview Removefromsuperview];    }//5, finally add the view of the sub-controller to Contentview, display columnVC.view.frame = _contentview.bounds;    [Self.contentview AddSubview:columnVC.view];    NSLog (@ "%@", Self.contentview); Before adding to the Mainview, get the Mainview navigation Controller's sub-controller and remove it (if any), then the new column corresponding to the sub-controller to add to the navigation Controller container, note that here can be used in the dictionary to remember all the instantiated column sub-controller, This avoids the alloc of creating a new column controller each time, but only needs to remove the last instantiated sub-controller from the dictionary according to the class name @end


Column data Model Column.h

  column.h//  19_ Drawer effect _ imitation netease////  Created by Beyond on 14-8-1.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <foundation/foundation.h>//data Model represents a column @interface column:nsobject//column name @property (nonatomic,copy) NSString *columnname;//column Picture name @property (nonatomic,copy) nsstring *columnimgname;//column corresponding to the controller's class name @property (Nonatomic, Copy) NSString *columnclassname;//UI control with weak, string with copy, other objects with strong//provides a class method, which is a constructor that returns the object that encapsulates the data (return ID also) + (Column *) Columnnamed: (NSString *) columnName imgname: (nsstring*) columnimgname className: (NSString *) columnclassname; @end




Column data Model COLUMN.M

  column.m//  19_ Drawer effect _ imitation netease////  Created by Beyond on 14-8-1.//  Copyright (c) 2014 Com.beyond. The All rights reserved.//  data Model represents a column #import "Column.h" @implementation column//Returns an object instance with the column corresponding controller name + (column *) Columnnamed: (NSString *) columnName imgname: (NSString *) columnimgname className: (NSString *) columnclassname{    // To use the self    Column *column = ["Self alloc]init] for compatibility subclasses;    Column.columnname = ColumnName;    Column.columnimgname = Columnimgname;    Column.columnclassname = Columnclassname;    return column;} @end




the left controller defines the protocol LeftTableViewControllerDelegate.h

  lefttableviewcontrollerdelegate.h//  19_ Drawer effect _ imitation netease////  Created by Beyond on 14-8-1.//  Copyright ( c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> #import "Column.h"//The left controller defines the proxy/protocol it by calling its own member property (that is, the proxy) This method, the data is passed out (to its agent to use)  (in fact, the host controller wants data, so the host controller in the left controller when instantiating, to set the left side of the controller corresponding to the main controller itself) @protocol Lefttableviewcontrollerdelegate <nsobject>-(void) lefttableviewrowclicked: (Column *) columnselected; @end

LeftTableViewController.h

  lefttableviewcontroller.h//  19_ Drawer effect _ imitation netease////  Created by Beyond on 14-8-1.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <uikit/uikit.h>//Advance declaration of the agreement @protocol Lefttableviewcontrollerdelegate , @interface lefttableviewcontroller:uitableviewcontroller//Agent with weak, to prevent the loop problem, can be any type, but must abide by the protocol @property (Nonatomic, weak) id<lefttableviewcontrollerdelegate> delegate; @end


Lefttableviewcontroller.m

lefttableviewcontroller.m//19_ Drawer Effect _ imitation NetEase////Created by Beyond on 14-8-1.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "LeftTableViewController.h" #import "Column.h" #import " LeftTableViewControllerDelegate.h "@interface Lefttableviewcontroller () {//column array, which holds all the column objects in the left column list Nsarray *_arr;}    @end @implementation lefttableviewcontroller-(void) viewdidload{[Super Viewdidload];    News column Column *newscolumn = [Column columnnamed:@ "News" imgname:@ "News.png" classname:@ "Newsviewcontroller"];    Image column Column *piccolumn = [column columnnamed:@ "Picture" imgname:@ "Pic.png" classname:@ "Picviewcontroller"];    Image column Column *commentcolumn = [column columnnamed:@ "thread" imgname:@ "Comment.png" classname:@ "Commentviewcontroller"];    In the future to add columns, as long as the change here can//will be Column object, all-in-one add to the non-variable group _arr = @[newscolumn,piccolumn,commentcolumn]; }-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return _arr.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *cellid = @ "L    EFTVC "; The following dequeue can only be used in storyboard or xib//UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid forIn    Dexpath:indexpath];        UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid]; if (cell = = nil) {cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellid]    ;    }//Set unique data Column *column = _arr[indexpath.row];    Cell.textLabel.text = Column.columnname;    Cell.imageView.image = [UIImage imageNamed:column.columnImgName]; return cell;} When clicking on a line, the main view in the master control must show the contents of the corresponding column, so the column controller of the row corresponding to the click must be instantiated, and the agent can be used to transfer the data to the agent using the method of adding to the navigation controller, the proxy is actually the main controller-(void) TableView: ( UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Cancel the default click Highlight Color First [TableView Deselectrowat    Indexpath:indexpath Animated:yes]; The Data Model (column) of the corresponding row is fetched column *column =_arr[indexpath.row]; if ([Self.delegate respondstoselector: @selector (lefttableviewrowclicked:)]) {//Pass data to the host controller Beyondviewcontro    Ller, through the agent//Key code ~ [Self.delegate Lefttableviewrowclicked:column]; }} @end


Rightviewcontroller.xib



Newsviewcontroller.xib



Picviewcontroller.xib



Commentviewcontroller.xib













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.