其實(shí)這種開(kāi)源框架github上已經(jīng)好多好多了,但是總是沒(méi)找到適合自己的,找了好久才找到。(我的要求,即可以左右滑動(dòng),也可以點(diǎn)擊滑動(dòng),并且可拓展性好。)下面總結(jié)兩種,也是我自己用的。
1.IIViewDeckController,這是我在下載ShareSDK的時(shí)候,發(fā)現(xiàn)他的demo里用的這種,感覺(jué)很不錯(cuò),擠拿過(guò)來(lái)用了。
在AppDelegate.m中
#import "CenterViewController.h" //中間的視圖控制器
#import "LeftViewController.h" //左邊的視圖控制器
#import "RightViewController.h" //右邊的視圖控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CenterViewController *centerVC = [[CenterViewController alloc] init];
UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:centerVC];
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
self.viewController = [[IIViewDeckController alloc] initWithCenterViewController:centerNav leftViewController:leftVC rightViewController:rightVC];//可以只添加左邊,也可以只添加右邊,具體看里面的代碼
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
在CenterViewController.m中
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rightBtn setTitle:@"right" forState:UIControlStateNormal];
rightBtn.frame = CGRectMake(0.0, 0.0, 53.0, 30.0);
[rightBtn addTarget:self action:@selector(rightButtonClickHandler:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)rightButtonClickHandler:(UIButton *)button
{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[app.viewController toggleRightViewAnimated:YES];
}
2.叫MKDSlideViewController
在AppDelegate.m中
//MainViewController為UITabBarControllers,里面放了viewControllers
MainViewController *main = [[MainViewController alloc] init];
LeftViewController *left = [[LeftViewController alloc] init];
RightViewController *right = [[RightViewController alloc] init];
_slideViewController = [[MKDSlideViewController alloc] initWithMainViewController:main];
_slideViewController.leftViewController = left;
_slideViewController.rightViewController = right;
self.window.rootViewController = self.slideViewController;
在控制器的.m文件中
- (void)viewDidLoad
{
[super viewDidLoad];
//
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, 44, 44);
[leftBtn setTitle:@"左" forState:UIControlStateNormal];
[leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftItemClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftItem;
}
#pragma mark - Action Methods
//這里用的是通知
-(void)leftItemClick
{
[[NSNotificationCenter defaultCenter] postNotificationName:kShowLeftControllertNotification object:nil];
}
具體的源碼github里可以自己下載。