2019独角兽企业重金招聘Python工程师标准>>>

自学iOS也有一段时间了,期间发现iOS和Android一样,有很多非常优秀的开源库可以使用。但无奈国内几乎没有太多关于此方面资料,唯有在Github上摸索。今天就写一篇关于PKRevealController的使用。本文章假定你已经具有一定的Objective-C开发技术技术,若需要入门教程请咨询谷歌君。

PKRevealController是什么

PKRevealController是由ZUUIRevealController改进而来,是一个简单、漂亮的开源库。实现了Facebook iOS客户端左右两边侧边菜单栏的效果(如下图)

  

其实,在Google上搜索『facebook like side menu』可以搜到一大堆可以实现的方案,其中有不少非常不错的实现方式。但我这篇文章中选择PKRevealController来演示。因为看了几种不同的实现方案之后,发现还是PKRevealController的实现方式比较简单、易用,而且最终效果和Facebook的效果高度一致。

其Github主页上对于它的说明如下:

PKRevealController (ex. ZUUIRevealController) is a delightful view controller container for iOS, enabling you to present multiple controllers on top of one another.

其主要的特点有:

  • Proper view controller containment usage
  • Elegant block API
  • Supports both left and right sided view presentation
  • Works on both iPhones & iPads
  • Supports landscape & portrait orientations

PKRevealController使用概要

  1. 从Github下载该项目,找到其中的PKRevealController/Controller文件夹,把它拖到项目中去就可以了

  2. 在任何一个你想要使用它的地方记得导入 #import "PKRevealController.h"

  3. 在你的项目AppDelegate.h里面声明一个PKRevealController对象

    @property (nonatomic, strong) PKRevealController *revealController;

    之后在AppDelegate.m中适当的初始化(后面详解)

    self.revealController = [PKRevealController revealControllerWithFrontViewController:frontViewController leftViewController:leftViewController options:nil];
  4. 在左边或者右边菜单栏里面现实内容,并在其中实现点击以后的切换效果即可

PKRevealController使用详解

为了能够演示如何使用PKRevealController,简单新建一个如下图所示的项目。运行之后可以看到主界面,在其主界面左上角导航栏具有一个按钮,点击之后即可查看菜单。菜单种有两项分别位:Home、Profile。点击Home返回主页面,点击Profile则显示个人信息页面。

  

  1. 打开Xcode并新建一个EmptyApplication,将其命名为PKRevealControllerDemo

  2. 下载PKRevealController项目并将其中的PKRevealController/Controller文件夹复制到项目中

  3. 打开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
  4. 新建用以显示主界面的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];
    }
  5. 新建用以显示左边菜单栏的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];
    }
  6. 新建用以显示个人信息页面的Controller,将其命名为ProfileViewController,其内容大致于MainFaceController类似,因此就不再详细描述。(请看代码)

  7. 回到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;
    }

完整代码下载:

  1. https://git.oschina.net/barrywey/PKRevealController-Demo

  2. https://github.com/barrywey/PKRevealController-Demo

转载于:https://my.oschina.net/u/1244672/blog/538983

iOS开源库PKRevealController的使用相关推荐

  1. 27个iOS开源库,让你的开发坐上火箭吧

    本文翻译自Medium,原作者是Paweł Białecki,原文 27个iOS开源库,让你的开发坐上火箭吧 你不会想错过他们,真的. 我爱开源. 并且我喜欢开发者们,把他们宝贵的私人时间用来创造神奇 ...

  2. ios开发——27个iOS开源库,让你的开发坐上火箭吧

    本文翻译自Medium,原作者是Paweł Białecki,原文 27个iOS开源库,让你的开发坐上火箭吧 你不会想错过他们,真的. 我爱开源. 并且我喜欢开发者们,把他们宝贵的私人时间用来创造神奇 ...

  3. 33个2017年必须了解的iOS开源库

    原文 本文翻译自Medium,原作者为 Paweł Białecki 照片版权:(Unsplash/Markus Pe) 你好,iOS 开发者们!我的名字叫 Paweł,我是一个独立 iOS 开发者, ...

  4. fir.im Weekly - 2017 年必须了解的 iOS 开源库

    放假的脚步临近,每个人都在期待一个愉悦的春节假期.最近,@张嘉夫 分享了一篇 Medium 上的文章<33 个 2017 年必须了解的 iOS 开源库>,总结了 2016 年最棒的 iOS ...

  5. 33 个 2017 年必须了解的 iOS 开源库

    你好,iOS 开发者们!我的名字叫 Paweł,我是一个独立 iOS 开发者,并且是 Enter Universe 的作者. 接近两年前我发布了 27 个iOS开源库,让你的开发坐上火箭吧.这是我在这 ...

  6. 27个提升效率的iOS开源库推荐

    2019独角兽企业重金招聘Python工程师标准>>> 我热爱开源,更喜爱那些花费宝贵的业余时间来创造奇迹的开发者们,感谢他们将自己辛苦劳动的成果无偿分享给大家.开源作者和贡献者们, ...

  7. 10个有用的第三方iOS开源库

    CocoaPods 地址:https://github.com/CocoaPods/CocoaPods 教程:http://www.raywenderlich.com/12139/introducti ...

  8. 33 个 2017 年必须了解的 iOS 开源库(包含swift)

    1.IGListKit,作者是Instagram Engineering Instagram 程序员做的,IGListKit 是数据驱动的 UICollectionView 框架,为了构建快速和可扩展 ...

  9. 33 款驰骋 2017 的 iOS 开源库

    文/Paweł Białecki 译/Mantra 原文:https://medium.com/app-coder-io/33-ios-open-source-libraries-that-will- ...

最新文章

  1. 创建一个触发器新增字段的时候设置某个字段的值
  2. C++源码的调用图生成
  3. sortingOrder,sortingLayer
  4. SharePoint Hello World Web Part
  5. linux 清空nat,linux 命令iptables -t nat
  6. excel保存超过15位数据不变科学计数法的方法
  7. 汇编指令mrs_专题1:电子工程师 之 软件】 之 【8.arm指令】
  8. linux修改last权限,Linux常用命令2/3(有关用户、权限管理的命令)--Unix/Linux操作系统04...
  9. Cesium 角度计算
  10. 编程思想 之「泛型」
  11. 【Transformers】第 5 章 :文本生成
  12. AB测试——流程介绍(设计实验)
  13. 《互联网理财一册通》一一1.3 开通网上银行
  14. 深入理解jvm(转)
  15. LaTeX中一些常用符号及编写技巧
  16. IDEA拉取Git代码问题
  17. Docsify 创建文档网站
  18. 1:1真人手办模型如何制作
  19. 3dsMax显示运动路径却没有显示的一种情况
  20. 安全隐私协议合规的那些事

热门文章

  1. Eclipse中导入Java项目出现“No projects are found to import”
  2. jsoup 去除html标签,如何使用jsoup取消注释html标签
  3. android 缓存头像,android 实现类似微信缓存和即时更新好友头像示例
  4. php7与apache整合,apache集成php7.3.5的详细步骤
  5. linux用户命令快捷链接,linux简单命令
  6. web api、获取DOM元素的方式、事件理解、click事件在移动端300ms延时、事件对象、事件委托、常见事件类型
  7. 流程控制介绍,顺序结构、分支结构、循环结构、Promise对象、throw语句、break和continue关键字
  8. 国潮正当时——2021百度国货用户洞察
  9. 以后再想大数据杀熟就没那么容易了
  10. 2020年上半年家电市场报告