MsgViewController.m
#import "MsgViewController.h"
#import "AppDelegate.h"
@interface MsgViewController ()
@end
@implementation MsgViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(showLeft:)];
}
- (void)showLeft:(UIButton *)button{
//1.获取App单例的delegate属性 --> AppDelegate类对应的单例对象
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
//2.
[appDelegate.menuVC showLeftVC];
}
@end
AppDelegate.h
#import <UIKit/UIKit.h>
#import "MenuViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic)MenuViewController *menuVC;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "MsgViewController.h"
#import "ContactsViewController.h"
#import "DynamicViewController.h"
#import "LeftViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
/*——————————————————————————————————————————————————————————————————————————————-*/
//1.视图控制器
MsgViewController *msg = [[MsgViewController alloc]init];
ContactsViewController *contacts = [[ContactsViewController alloc]init];
DynamicViewController *dynamic = [[DynamicViewController alloc]init];
//2.导航控制器
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:msg];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:contacts];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:dynamic];
//定制item
msg.title = @"消息";
contacts.title = @"联系人";
dynamic.title = @"动态";
msg.view.backgroundColor = [UIColor redColor];
contacts.view.backgroundColor = [UIColor grayColor];
dynamic.view.backgroundColor = [UIColor orangeColor];
msg.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem: UITabBarSystemItemRecents tag:101];
contacts.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:102];
dynamic.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:103];
//3.标签控制器
UITabBarController *tbc = [[UITabBarController alloc]init];
tbc.viewControllers = @[nav1,nav2,nav3];
// self.window.rootViewController = tbc;
LeftViewController *leftVC = [[LeftViewController alloc]init];
_menuVC = [[MenuViewController alloc]initWithCenterViewController:tbc leftViewController:leftVC];
self.window.rootViewController = _menuVC;
return YES;
}
@end
MenuViewController.h
#import <UIKit/UIKit.h>
@interface MenuViewController : UIViewController
@property (nonatomic,strong)UIViewController *leftViewController;
@property (nonatomic,strong)UIViewController *centerViewController;
/**
* 通过视图控制器创建侧滑控制器
*
* @param centerViewController 中心视图控制器
* @param leftViewController 左侧视图控制器
*
* @return 侧滑控制器
*/
- (instancetype)initWithCenterViewController:(UIViewController *)centerViewController
leftViewController:(UIViewController *)leftViewController;
/**
* 左侧滑和还原功能
*/
- (void)showLeftVC;
/**
* 显示中心控制器
*/
- (void)showCenterVC;
@end
MenuViewController.m
#import "MenuViewController.h"
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KSCreenHeight [UIScreen mainScreen].bounds.size.height
#define KLeftSpace KScreenWidth/5*4
@interface MenuViewController ()
@end
@implementation MenuViewController
- (instancetype)initWithCenterViewController:(UIViewController *)centerViewController
leftViewController:(UIViewController *)leftViewController{
if (self = [super init]) {
_centerViewController = centerViewController;
_leftViewController = leftViewController;
/**
* 现在位于视图控制器MenuViewController的M文件中
父视图就是self.view 也就是MenuVC的根视图
left view 在下 center view 在上 当调用侧滑事件时,center view 向右平移
*/
}
return self;
}
- (instancetype)initWithCenterViewController:(UIViewController *)centerViewController{
if (self = [super init]) {
_centerViewController = centerViewController;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
if (_leftViewController) {
[self.view addSubview:_leftViewController.view];
}
[self.view addSubview:_centerViewController.view];
}
- (void)showLeftVC{
//判断中央控制器的view的偏移量
//如果为0 --> 没有侧滑 --> 开始侧滑
if (_centerViewController.view.frame.origin.x == 0) {
[UIView animateWithDuration:0.3 animations:^{
_centerViewController.view.frame = CGRectMake(KLeftSpace, 0, _centerViewController.view.frame.size.width, _centerViewController.view.frame.size.height);
}];
}
else{
[self showCenterVC];
}
}
- (void)showCenterVC{
[UIView animateWithDuration:0.3 animations:^{
_centerViewController.view.frame = self.view.bounds;
}];
}
@end

屏幕快照 2016-03-09 下午6.47.24.png

屏幕快照 2016-03-09 下午6.47.33.png

屏幕快照 2016-03-09 下午7.12.57.png