Drawer effect IOS --- similar To Netease client --- initially complete encapsulation --- easy to use --- IOS

Source: Internet
Author: User

After reading the effects of several drawers, I thought that most of them were complicated and encapsulated by myself. this is mainly for your own project services. It is encapsulated in the style of the Netease client and does not provide an interface to change the intermediate View frame. If you are interested, you can try to add a frame interface. (encapsulated when ARC is enabled)

Core functions: Click cell in the left drawer to close the drawer. A new intermediate controller or an existing controller can be introduced.

When the drawer is opened, click the middle view to reclaim the drawer.

Note: The intermediate view must be navigationcontroller, and the left and right buttons of items must be rewritten in the intermediate controller class.

Must introduce my category. It is best to put it in the pch file,

This is the initial version, and there are still some imperfections. We don't like it, we don't want to spray it. animation and other functions have not yet been added, mainly to complete the core functions of the drawer.

--------------------------------------------------------- For reprinting, indicate the source --------------------------------------------------------

-------------------------------------------- Http://blog.csdn.net/u013082522 ------------------------------------------

-------------------- Sample code download http://download.csdn.net/detail/u013082522/6952231 --------------

DrawerViewController. h

# Import <UIKit/UIKit. h> @ protocol DrawerViewDelegate <NSObject> // protocol, which is not used currently. -(void) manageLeftDrawer;-(void) manageRightDrawer; @ end @ interface DrawerViewController: UIViewController @ property (nonatomic, retain) UIViewController * LeftDrawerVC; @ property (nonatomic, retain) UIViewController * CenterDrawerVC; @ property (nonatomic, retain) UIViewController * initialize; // initialization method-(id) initialize :( UIViewController *) leftViewController centerViewController :( UIViewController *) centerViewController rightViewController :( UIViewController *) rightViewController; // click the left and right buttons-(void) tapLeftDrawerButton;-(void) tapRightDrawerButton; // close the left drawer for easy calling-(void) closeLeftDrawer; // close the right drawer, convenient call-(void) closeRightDrawer; @ end
DrawerViewController. m

# Import "DrawerViewController. h "@ interface DrawerViewController () @ property (nonatomic, assign) BOOL IsLeft; @ property (nonatomic, assign) BOOL IsRight; @ property (nonatomic, assign) BOOL LeftDrawerIsShow; @ property (nonatomic, assign) BOOL RightDrawerIsShow; @ property (nonatomic, retain) implements * tapLeft; @ property (nonatomic, retain) implements * tapRight; @ property (nonatomic, retain) UISwipeGest UreRecognizer * swipeLeft; @ end @ implementation DrawerViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: bundle: role]; if (self) {self. view. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "11.jpg"];} return self;}-(id) initWithLeftViewController :( UIViewController *) leftViewContro Roller centerViewController :( UIViewController *) centerViewController rightViewController :( UIViewController *) rightViewController {self = [super init]; if (self) {// load the Left view [self setLeftDrawerVC: leftViewController]; [self. view addSubview: leftViewController. view]; // load the right view [self setRightDrawerVC: rightViewController]; [self. view addSubview: rightViewController. view]; // load the intermediate view .... the intermediate view must end with [self setCente RDrawerVC: centerViewController]; // [self. view addSubview: _ CenterDrawerVC. view]; // you can specify a Boolean self for the left and right drawers. isLeft = YES; self. isRight = YES; // the initial value of the specified state self. leftDrawerIsShow = NO; self. rightDrawerIsShow = NO; // Add the left controller as a subcontroller so that the Left controller can find self [self addChildViewController: leftViewController]; // Add the right controller as a subcontroller, in this way, the Controller on the right can find self [self addChildViewController: rightViewController];} return self;} // rewrite the set method for setting the intermediate view- (Void) setCenterDrawerVC :( UIViewController *) CenterDrawerVC {if (_ CenterDrawerVC = nil) {_ CenterDrawerVC = CenterDrawerVC; [self. view addSubview: _ CenterDrawerVC. view]; [self addChildViewController: _ CenterDrawerVC]; NSLog (@ "");} else {UIViewController * oldVC = _ CenterDrawerVC; _ CenterDrawerVC = CenterDrawerVC; [self. view addSubview: _ CenterDrawerVC. view]; [oldVC removeFromParentViewController]; [OldVC. view removeFromSuperview]; [self addChildViewController: _ CenterDrawerVC];}-(void) addSwipeGeustureToLeft {if (_ LeftDrawerIsShow) {self. swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (tapLeftDrawerButton)]; [_ swipeLeft setDirection: Unknown]; [_ CenterDrawerVC. view addGestureRecognizer: self. swipeLeft];} else {if (self. swipeL Eft! = Nil) {[_ CenterDrawerVC. view removeGestureRecognizer: _ swipeLeft] ;}}# pragma click gesture method // when the left drawer is open, add a click gesture on the intermediate view, click back left drawer-(void) AddTapGestureToLeft {if (_ LeftDrawerIsShow) {self. tapLeft = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapLeftDrawerButton)]; [_ CenterDrawerVC. view addGestureRecognizer: self. tapLeft];} else {if (self. tapLeft! = Nil) {[_ CenterDrawerVC. view removeGestureRecognizer: _ tapLeft] ;}}// when the right drawer is open, add a gesture in the middle view and click withdraw right drawer-(void) addTapGestureToRight {if (_ RightDrawerIsShow) {self. tapRight = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (tapRightDrawerButton)]; [_ CenterDrawerVC. view addGestureRecognizer: self. tapRight];} else {if (self. tapRight! = Nil) {[_ CenterDrawerVC. view removeGestureRecognizer: _ tapRight] ;}}# click events of buttons around pragma // click events on the left-side-(void) tapLeftDrawerButton {_ RightDrawerVC. view. hidden = YES; _ LeftDrawerVC. view. hidden = NO; if (! Self. isLeft) {_ CenterDrawerVC. view. frame = CGRectMake (0, 0,320,568); _ IsLeft = YES; _ LeftDrawerIsShow = NO; [self AddTapGestureToLeft]; [self addSwipeGeustureToLeft];} else {_ CenterDrawerVC. view. frame = CGRectMake (250, 50,320,400); _ IsLeft = NO; _ LeftDrawerIsShow = YES; [self AddTapGestureToLeft]; [self addSwipeGeustureToLeft] ;}}// click the event on the right-(void) tapRightDrawerButton {_ LeftDrawerVC. view. hidden = YES; _ RightDrawerVC. view. hidden = NO; if (! Self. isRight) {_ CenterDrawerVC. view. frame = CGRectMake (0, 0,320,568); _ IsRight = YES; _ RightDrawerIsShow = NO; [self AddTapGestureToRight];} else {_ CenterDrawerVC. view. frame = CGRectMake (-250, 60,320,400); _ RightDrawerIsShow = YES; _ IsRight = NO; [self AddTapGestureToRight];} # pragma quick way to close left and right drawers // close left drawers for easy calling-(void) closeLeftDrawer {_ CenterDrawerVC. view. frame = CGRectMake (0, 0,320,568); _ IsLeft = YES; _ LeftDrawerIsShow = NO; [self AddTapGestureToLeft];} // close the right drawer for easy call-(void) closeRightDrawer {_ CenterDrawerVC. view. frame = CGRectMake (0, 0,320,568); _ IsRight = YES; _ RightDrawerIsShow = NO; [self AddTapGestureToRight];}-(void) viewDidLoad {[super viewDidLoad];} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
Class Object. h file

#import <UIKit/UIKit.h>#import "DrawerViewController.h"@interface UIViewController (MyDrawer)@property(nonatomic,strong,readonly)DrawerViewController *xukunDrawerViewController;@end
Category. m file
@implementation UIViewController (MyDrawer)-(DrawerViewController *)xukunDrawerViewController{        if([self.parentViewController isKindOfClass:[DrawerViewController class]]){                return (DrawerViewController*)self.parentViewController;            }else if([self.parentViewController isKindOfClass:[UINavigationController class]] &&            [self.parentViewController.parentViewController isKindOfClass:[DrawerViewController class]]){                return (DrawerViewController*)[self.parentViewController parentViewController];            }else{                return nil;    }}@end


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.