这几天闲着没事,看了几个抽屉效果,总觉得大都写的有些复杂,便自己封装了一个.主要为了自己项目服务,主要按照网易客户端的样式封装,未提供更改中间视图frame的接口,有兴趣的话可以自己试着添加一个frame接口即可. (在ARC开启的情况下封装的)
核心功能:点击左抽屉中的cell,关闭抽屉,可以推出新的中间控制器,或者原本存在的控制器.
抽屉开启的情况下,点击中间视图,收回抽屉.
注意事项:中间视图必须是navigationcontroller,并自己在中间控制器类中重写item的左右button.
必须引入我的类目.最好放进pch文件中,
这是初始版本,肯定还有不尽完善的地方,不喜勿喷.动画等功能还未加入,主要完成抽屉的核心功能.
-------------------------------------------------------转载请注明出处--------------------------------------------------------
--------------------------------------------http://blog.csdn.net/u013082522------------------------------------------
--------------------示例代码下载http://download.csdn.net/detail/u013082522/6952231--------------
DrawerViewController.h
#import <UIKit/UIKit.h> @protocol DrawerViewDelegate <NSObject>//协议,暂时未使用. - (void)manageLeftDrawer; - (void)manageRightDrawer; @end @interface DrawerViewController : UIViewController @property(nonatomic,retain)UIViewController *LeftDrawerVC; @property(nonatomic,retain)UIViewController *CenterDrawerVC; @property(nonatomic,retain)UIViewController *RightDrawerVC; //初始化方法 - (id)initWithLeftViewController:(UIViewController *)leftViewController centerViewController:(UIViewController *)centerViewController rightViewController:(UIViewController *)rightViewController; //点击左右按钮 - (void)tapLeftDrawerButton; - (void)tapRightDrawerButton; //关闭左抽屉,方便调用 - (void)closeLeftDrawer; //关闭右抽屉,方便调用 - (void)closeRightDrawer; @endDrawerViewController.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)UITapGestureRecognizer *tapLeft; @property(nonatomic,retain)UITapGestureRecognizer *tapRight; @property(nonatomic,retain)UISwipeGestureRecognizer *swipeLeft; @end @implementation DrawerViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"11.jpg"]]; } return self; } - (id)initWithLeftViewController:(UIViewController *)leftViewController centerViewController:(UIViewController *)centerViewController rightViewController:(UIViewController *)rightViewController { self = [super init]; if (self) { //加载左视图 [self setLeftDrawerVC:leftViewController]; [self.view addSubview:leftViewController.view]; //加载右视图 [self setRightDrawerVC:rightViewController]; [self.view addSubview:rightViewController.view]; //加载中间视图....中间视图一定要最后添加 [self setCenterDrawerVC:centerViewController]; //[self.view addSubview:_CenterDrawerVC.view]; //设置左右抽屉的任意一个布尔值 self.IsLeft = YES; self.IsRight = YES; //给定状态初始值 self.LeftDrawerIsShow = NO; self.RightDrawerIsShow = NO; //将左控制器添加为子控制器,这样左边控制器才可以找到self [self addChildViewController:leftViewController]; //将右控制器添加为子控制器,这样右边控制器才可以找到self [self addChildViewController:rightViewController]; } return self; } //重写设置中间视图的set方法 - (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:UISwipeGestureRecognizerDirectionDown]; [_CenterDrawerVC.view addGestureRecognizer:self.swipeLeft]; }else{ if (self.swipeLeft != nil) { [_CenterDrawerVC.view removeGestureRecognizer:_swipeLeft]; } } } #pragma 点击手势方法 //当左抽屉处于打开状态的时候,在中间视图上添加单击手势,点击收回左抽屉 - (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]; } } } //当右抽屉处于打开状态的时候,在中间视图上添加单击手势,点击收回右抽屉 - (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]; } } } #pragma 左右button的点击事件 //左边点击事件 -(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]; } } //右边点击事件 - (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 关闭左右抽屉的快捷方法 //关闭左抽屉,方便调用 - (void)closeLeftDrawer { _CenterDrawerVC.view.frame = CGRectMake(0, 0, 320, 568); _IsLeft = YES; _LeftDrawerIsShow = NO; [self AddTapGestureToLeft]; } //关闭右抽屉,方便调用 - (void)closeRightDrawer { _CenterDrawerVC.view.frame = CGRectMake(0, 0, 320, 568); _IsRight = YES; _RightDrawerIsShow = NO; [self AddTapGestureToRight]; } - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end类目的.h文件
#import <UIKit/UIKit.h> #import "DrawerViewController.h" @interface UIViewController (MyDrawer) @property(nonatomic,strong,readonly)DrawerViewController *xukunDrawerViewController; @end类目的.m文件
@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