自学iOS也有一段时间了,期间发现iOS和Android同样,有不少很是优秀的开源库可使用。但无奈国内几乎没有太多关于此方面资料,惟有在Github上摸索。今天就写一篇关于PKRevealController的使用。本文章假定你已经具备必定的Objective-C开发技术技术,若须要入门教程请咨询谷歌君。 git
PKRevealController是由ZUUIRevealController改进而来,是一个简单、漂亮的开源库。实现了Facebook iOS客户端左右两边侧边菜单栏的效果(以下图) github
其实,在Google上搜索『facebook like side menu』能够搜到一大堆能够实现的方案,其中有很多很是不错的实现方式。但我这篇文章中选择PKRevealController来演示。由于看了几种不一样的实现方案以后,发现仍是PKRevealController的实现方式比较简单、易用,并且最终效果和Facebook的效果高度一致。 app
其Github主页上对于它的说明以下: ide
PKRevealController (ex. ZUUIRevealController) is a delightful view controller container for iOS, enabling you to present multiple controllers on top of one another. 布局
其主要的特色有: atom
从Github下载该项目,找到其中的PKRevealController/Controller文件夹,把它拖到项目中去就能够了 spa
在任何一个你想要使用它的地方记得导入 #import "PKRevealController.h" .net
在你的项目AppDelegate.h里面声明一个PKRevealController对象 设计
@property (nonatomic, strong) PKRevealController *revealController;
以后在AppDelegate.m中适当的初始化(后面详解) code
self.revealController = [PKRevealController revealControllerWithFrontViewController:frontViewController leftViewController:leftViewController options:nil];
在左边或者右边菜单栏里面现实内容,并在其中实现点击之后的切换效果便可
为了可以演示如何使用PKRevealController,简单新建一个以下图所示的项目。运行以后能够看到主界面,在其主界面左上角导航栏具备一个按钮,点击以后便可查看菜单。菜单种有两项分别位:Home、Profile。点击Home返回主页面,点击Profile则显示我的信息页面。
打开Xcode并新建一个EmptyApplication,将其命名为PKRevealControllerDemo
下载PKRevealController项目并将其中的PKRevealController/Controller文件夹复制到项目中
打开RevealControllerAppDelegate.h文件,在其中引入#import "PKRevealController.h",以后声明一个PKRevealController类型的对象命名为revealController
#import <UIKit/UIKit.h> #import "PKRevealController.h" @interface RevealControllerAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong,nonatomic) PKRevealController *revealController; @end
新建用以显示主界面的Controller,将其命名为MainFaceController。为了简单起见,界面设计等使用xib布局就好。在界面上随便拖动一个组建,并显示相关数据。为了简便,我在界面上放置一个UILabel组件,并在其中显示问候信息。核心代码以下:
- (void)viewDidLoad { [super viewDidLoad]; //设置当前标题 [self setTitle:@"Home"]; //设置标题栏上左边的按钮 UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showLeftView)]; self.navigationItem.leftBarButtonItem = btnLeft; } //按钮点击事件 - (void) showLeftView { [self.navigationController.revealController showViewController:self.navigationController.revealController.leftViewController]; }
新建用以显示左边菜单栏的Controller,将其命名为LeftFaceController。通常状况下,菜单栏可使用UITableView实现。表格数据填充参考:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellReuseIndentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } switch (indexPath.row) { case 0: [cell.textLabel setText:@"Home"]; break; case 1: [cell.textLabel setText:@"Profile"]; break; } return cell; }
表格中点击事件参考:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UINavigationController *frontViewController = nil; switch (indexPath.row) { case 0: //home frontViewController = [[UINavigationController alloc] initWithRootViewController:self.mainFaceController]; break; case 1: //profile frontViewController = [[UINavigationController alloc] initWithRootViewController:self.profileViewController]; break; } [self.revealController setFrontViewController:frontViewController]; [self.revealController showViewController:self.revealController.frontViewController]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
新建用以显示我的信息页面的Controller,将其命名为ProfileViewController,其内容大体于MainFaceController相似,所以就再也不详细描述。(请看代码)
回到RevealControllerAppDelegate.m,在其中并初始化主界面MainFaceController、左边菜单栏LeftFaceController、PKRevealController等对象,并将其做为rootViewController展现给用户
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //主界面 MainFaceController* mainFaceController = [[MainFaceController alloc] init]; //菜单栏 LeftFaceController* leftFaceController = [[LeftFaceController alloc] init]; //构造PKRevealController对象 UINavigationController *frontViewController = [[UINavigationController alloc] initWithRootViewController:mainFaceController]; self.revealController = [PKRevealController revealControllerWithFrontViewController:frontViewController leftViewController:leftFaceController options:nil]; //将其PKRevealController对象做为RootViewController self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = self.revealController; [self.window makeKeyAndVisible]; return YES; }