iOS开源库PKRevealController的使用
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使用概要
從Github下載該項目,找到其中的PKRevealController/Controller文件夾,把它拖到項目中去就可以了
在任何一個你想要使用它的地方記得導入?#import "PKRevealController.h"
在你的項目AppDelegate.h里面聲明一個PKRevealController對象
@property (nonatomic, strong) PKRevealController *revealController;之后在AppDelegate.m中適當的初始化(后面詳解)
self.revealController = [PKRevealController revealControllerWithFrontViewController:frontViewController leftViewController:leftViewController options:nil];在左邊或者右邊菜單欄里面現實內容,并在其中實現點擊以后的切換效果即可
PKRevealController使用詳解
為了能夠演示如何使用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; }完整代碼下載:
https://git.oschina.net/barrywey/PKRevealController-Demo
https://github.com/barrywey/PKRevealController-Demo
轉載于:https://my.oschina.net/u/1244672/blog/538983
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的iOS开源库PKRevealController的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《从零开始学Swift》学习笔记(Day
- 下一篇: 设置ArcGIS的外观改回到出厂